这是我的未排序数组:
P B A
5 125 400
2 102 145
3 56 200
6 65 200
7 30 200
4 148 300
1 135 0
这是我当前数组排序后的内容,这是我得到的输出
P B A
1 135 0
2 102 145
3 56 200
6 65 200
7 30 200
4 148 300
5 125 400
我希望输出看起来像这样
P B A
1 135 0
2 102 145
7 30 200
3 56 200
6 65 200
4 148 300
5 125 400
这是我目前的代码
Arrays.sort(myArr, new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[2], o2[2]);
}
});
最佳答案
您需要进一步指定比较器
。目前,您仅按第三个元素对数组进行排序。
Arrays.sort(myArr, new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
int ret = Integer.compare(o1[2], o2[2]);
// if the entries are equal at index 2, compare index 1
if (0 == ret) {
ret = Integer.compareTo(o1[1], o2[1]);
}
return (ret);
}
});
关于java - 对 double 组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29610549/