Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6657181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:44:09+00:00 2026-05-26T01:44:09+00:00

Doing homework and I’m stuck. Let’s say I have an array colors: [blue, orange,

  • 0

Doing homework and I’m stuck.

Let’s say I have an array colors:

["blue", "orange", "green", "black", "red" ]

and these colors occur in a text. When the color occurs there is another array that stores the line number in another array (position array).

[17,4,5,8,8]

Now I want to print by ascending line occurrence so the output would be:

orange
green
black
red
blue

I use the Arrays.sort() to sort the position array.
I believe that this should be done using position.

For example, for printing orange, there is a relation of the sorted array with the position of the color on the array colors.

Can you point me in some direction?

As I started to learn java this as to be done the simplest way possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:44:09+00:00Added an answer on May 26, 2026 at 1:44 am
    1. You need to associate the indices with each other. I would recommend you to do it in a class (maybe a Pair class with the attributes String color and int line).

      class Pair {
          public String color;
          public int line;
          public Pair(String color, int line) {
              this.color = color;
              this.line  = line;
          }
      }
      
    2. Build up an array (or List<Pair>) of the paired objects.

      String[] colors = new String[] {"blue", "orange", "green", "black", "red"};
      int[] lines     = new int[]    {    17,        4,       5,       8,     8};
      
      List<Pair> pairs = new LinkedList<Pair>();
      for (int i = 0; i < colors.length; i++)
          pairs.add(new Pair(colors[i], lines[i]));
      
    3. Sort the array of Pairs with an Comparator using Arrays.sort (or Collection.sort) method depending on your line attribute.

      Collections.sort(pairs, new Comparator<Pair>() {
          public int compare(Pair p1, Pair p2) {
              return Integer.valueOf(p2.line).compareTo(p1.line);
          }
      });
      

      Another option would be to implement Comparable<Pair> in Pair.

    4. Print the array using a loop

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am doing homework and I am stuck on one spot. I have
I'm doing some homework and while I have some experience with SML, Haskell has
As question states, I am doing homework which have 2 variables BOOK, and MAGAZINE
This is a homework problem that I have. I have been doing some research
I'm doing a C homework project and I'm incredibly lost. Essentially, I have to
I was doing homework in C# that required sorting an array. I could sort
Possible Duplicate: length of array in function argument Hi am doing homework and I
I'm doing a homework assignment in C. I have to build a calculator that
newbie doing Java homework here. I have one class named Album which contains the
I am doing a homework assignment and I am running into these issues. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.