I was sorting an 2D int array when doint a leetcode question https://leetcode.com/problems/merge-intervals/
First, I used custom comparator but one testcase gives me java.lang.IllegalArgumentException: Comparison method violates its general contract!
Arrays.sort(intervals,new Comparator<int[]>(){
@Override
public int compare(int[] a,int[] b){
return a[0]>b[0]? 1:-1;
}
});
However, lambda expression below does not show any Exception:
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
the failing testcase is something like this but array length is much much longer: [[3,4],[1,5],[64,66],[73,77],[90,94],[20,21],[84,87],[48,49],[80,80]]
I'm super confused and would appreciate if someone can show me the difference :)