Creating a Comparator
is the correct way to solve this. Whether you then use Arrays.sort()
or Collections.sort()
is up to you.
I would suggest the former avoids a conversion to a list and is thus preferable.
I recommend you don't solve this by implementing Comparable
in your object, as this sounds like a display issue and embedding a sort order in the object itself is probably not wise.
An example (untested) implementation might be:
public class ExampleComparator implements Comparator<YourObject> {
public int compare(YourObject o1, YourObject o2) {
if (o1 == null || o2 == null) {
throw new NullPointerException();
}
if (o1.getValue1() != o2.getValue1()) {
return Integer.compare(o1.getValue1(), o2.getValue1());
}
return Integer.compare(o1.getValue2(), o2.getValue2());
}
}