I have a list of Objects which are not comparable. I however would still like to sort this list of Objects based on a provided array of index positions. What is the fastest and most efficient way to sort this list? Here is an example:
List<Colour> list = new ArrayList<Colour>();
list.add(Colour.BLUE);
list.add(Colour.GREEN);
list.add(Colour.RED);
list.add(Colour.YELLOW);
list.add(Colour.GREEN);
int[] order = new int[] {3, 1, 2, 0, 4};
The final list should look like:
[YELLOW, GREEN, RED, BLUE, GREEN]
My specific requirement is for a solution in Java but I would be interested to know the solution in other languages as well.
Why don’t you just create the list from the indexes directly? No “sorting” is required.
Or C#: