1. 默认升序
2. 一般用法,对二维数组进行升序排列,第一个数字相同则按照第二个升序。
int[] a = [[1,4],[2,3],[6,9],[8,4]];
Arrays.sort(a,(o1,o2)->{
return o1[0] == o2[0] ? o1[1]-o2[1] : o1[0]- o2[0];
});
比较部分等价于
Arrays.sort(a,new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
// TODO Auto-generated method stub
return o1[0] == o2[0] ? o1[1]-o2[1] : o1[0]- o2[0];
}
});
compare(int[] x,int[] y) 来“比较x和y的大小”。
若返回“负数”,意味着“x比y小”;返回“零”,意味着“x等于y”;返回“正数”,意味着“x大于y”。
也就是说,有如下两种理解:
1. 返回正数意味着降序排列,返回负数意味着升序排列。
2. 想升序就return x-y;想降序就y-x。
=======================2022.1.18=======================