How can I sort Arrays in a Collection?

Viewed 2483

I have a a List of Objects. Those Objects have (amongst others) a private int Array (if it helps, I can transfer it into a List). There is a public Getter for this Array. All Arrays are the same size.

I want to sort the Object based on their Arrays like this:

Unsorted:
{[0, 1, 4, 5], 
 [0, 0, 2, 3],
 [0, 1, 1, 2]}

Sorted:
{[0, 0, 2, 3],
 [0, 1, 1, 2],
 [0, 1, 4, 5]}

In Words (it's called lexicographical):

  • compare the first int of each array
  • if they are equal, compare the next int of each array (and so on)
  • if they aren't equal the result of the comparison is the end result.

I manage to search them for e.g. only the first element of the array with a normal Comparator but I don't know how to search them for all.

7 Answers
Related