java Arrays.sort 二维数组 [英] java Arrays.sort 2d array
本文介绍了java Arrays.sort 二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望根据 [][0] 的值对以下数组进行排序
I am looking to sort the following array based on the values of [][0]
double[][] myArr = new double[mySize][2];
例如,myArr 的内容是:
so for example, myArr contents is:
1 5
13 1.55
12 100.6
12.1 .85
我希望它到达:
1 5
12 100.6
12.1 .85
13 1.55
我希望无需实现我自己的排序即可做到这一点.
I am looking to do this without having to implement my own sort.
推荐答案
Use Overloaded Arrays#Sort(T[] a, Comparator c) 将 Comparator 作为第二个参数.
Use Overloaded Arrays#Sort(T[] a, Comparator c) which takes Comparator as the second argument.
double[][] array= {
{1, 5},
{13, 1.55},
{12, 100.6},
{12.1, .85} };
java.util.Arrays.sort(array, new java.util.Comparator<double[]>() {
public int compare(double[] a, double[] b) {
return Double.compare(a[0], b[0]);
}
});
JAVA-8:代替那个大的比较器,我们可以使用如下的 lambda 函数 -
JAVA-8: Instead of that big comparator, we can use lambda function as following-
Arrays.sort(array, Comparator.comparingDouble(o -> o[0]));
这篇关于java Arrays.sort 二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文