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.
You need to associate the indices with each other. I would recommend you to do it in a class (maybe a
Pairclass with the attributesString colorandint line).Build up an array (or
List<Pair>) of the paired objects.Sort the array of
Pairs with anComparatorusingArrays.sort(orCollection.sort) method depending on yourlineattribute.Another option would be to implement
Comparable<Pair>inPair.Print the array using a loop