简体   繁体   English

排序 2D int 数组 JAVA

[英]sort 2D int array JAVA

I have one 2 dimensional array It's an array with 7 int in line and more lines.我有一个二维数组,它是一个包含 7 个 int 和更多行的数组。

int[][] new arr=new[7][100];

number in line is ranked.行中的数字排名。

I need a ranked array.我需要一个排名数组。

for example例如

9 4 15 22 32 47 50
1 5 9 12 19 25 36
22 23 25 29 36 55 99
1 5 11 12 19 25 36

after sort排序后

1 5 9 12 19 25 36
1 5 11 12 19 25 36
9 4 15 22 32 47 50
22 23 25 29 36 55 99

The simple approach to solved this problem is transform your 2D array into List of 1D array.解决这个问题的简单方法是将您的二维数组转换为一维数组列表。

List<int[]> list = new ArrayList<int[]>();
// add logic to transform your 2D array here

Then you can use Collections.sort() with custom Comparator function.然后您可以将 Collections.sort() 与自定义 Comparator 函数一起使用。

Collections.sort(list, new Comparator<int[]>() {
    public int compare(int []a,int []b) {
        for(int i=0;i<6;i++)
            if(a[i]!=b[i]) return a[i]-b[i];
        return a[6] - b[6];
    }
});

I would do something like this,or something similar:我会做这样的事情,或类似的事情:

I saw something similar in Stack Overflow: https://stackoverflow.com/a/15452462/8024829 .我在 Stack Overflow 中看到了类似的东西: https : //stackoverflow.com/a/15452462/8024829

 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]);
       }
   });

You can try this simple expression:你可以试试这个简单的表达式:

Arrays.sort(A, (a, b) -> a[0] - b[0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM