I want to sort a List<Pair<Long, Long>> list = new ArrayList<Pair<Long, Long>>();
according to both the indexes, firstly according to first index and if first indexes are equal then according to second index using Java 8 lambda function.
I could easily sort only by first index as:
Collections.sort(list,(o1, o2) -> o1.first < o2.first ? -1:0);
If I want to sort according to both the indexes
Collections.sort(list,(o1, o2) -> o1.first < o2.first ? -1 : o1.first == o2.first ? (o1.second < o2.second ? -1 : 0) : 0);
But I don't think its the right way to do it. Can someone please provide a better syntax?
Pair definition:
class Pair<Type1, Type2> {
Type1 first;
Type2 second;
Pair(Type1 f, Type2 s) {
this.first = f;
this.second = s;
}
public String toString() {
return "(" + this.first + ", " + this.second + ")";
}
}