0

is there a way to sort an array and store the result in a linked list with the original array positions referenced by the list?

so

0 bbb 1 aaa 2 ccc

would become a linked list

1 aaa 0 bbb 2 ccc

Thanks

1
  • How exactly do you want the array positions preserved? Commented Dec 21, 2011 at 8:44

2 Answers 2

3
MyClass[] array = new MyClass[n];
// fill the array
Arrays.sort(array);
List<MyClass> list = new LinkedList<Class>(Arrays.asList(array));
Sign up to request clarification or add additional context in comments.

Comments

0

You can just use a TreeMap:

final SortedMap<String, Integer> map = new TreeMap<String, Integer>(array.length);

for (int index = 0; index < array.length; i++)
    map.put(array[index], index);

The keys are therefore the strings, in order, and the positions in the original aray are the values. But storing a String and its origin position requires a dedicated structure. Or maybe the Map.Entry<String, Integer> entries for the map are enough for your needs. It depends on what you want to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.