I have a 2D array of type int i.e [][]intervals
Using Java 8 syntax, I can sort like this
Arrays.sort(intervals, (int[] o1, int[] o2) -> {
if (o1[1] != o2[1]) return o1[1] - o2[1];
return o1[0] - o2[0];
});
Basically, I understand I have the logic defined as the 2nd argument which is Comparator
I want to just for the sake of learning, do something like this
var c = Comparator.<int[]>.comparing(not able to put the 2 arguments properly here)
This also works, but I am not able to use the var and Comparator style above
Comparator<int[]> c = (int[] i1, int[] i2) -> {
if (i1[0] == i2[0]) return i1[1] - i2[1];
return i1[0] - i2[0];
}
I am unable to get the syntax right. I got it working as below as well, but that is not the Lambda style as I am trying to do above
var c = new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if (o1[1] != o2[1]) return o1[1] - o2[1];
return o1[0] - o2[0];
}
};
Arrays.sort(intervals,c);