简体   繁体   English

如何按字典顺序对二维数组进行排序?

[英]How to sort two dimensional array lexicographically?

Assuming we have a two-dimensional array as follows:假设我们有一个二维数组如下:

int[][] source = {
  {   3,  5,  6,  1},
  {   3,  3,  5, -6},
  {  -1, -3, -5, -6},
  { 124, 43, 55, -66}
};

how do we sort the multidimensional array source lexicographically ?我们如何按字典顺序对多维数组source进行排序?

So, as a result, I'd expect it to be:所以,结果,我希望它是:

[ [ -1, -3, -5,  -6], 
  [  3,  3,  5,  -6], 
  [  3,  5,  6,   1], 
  [124, 43, 55, -66] ]

a lot of questions on this site seem to only suggest sorting by the first element of each array or second, third etc. but not taking in consideration the entire array.这个网站上的很多问题似乎只建议按每个数组的第一个元素或第二个、第三个等排序,但没有考虑整个数组。

As of JDK9, there's a new method called Arrays.compare which allows you to compare two given arrays lexicographically . 从JDK9开始,有一个名为Arrays.compare的新方法,它允许您按字典顺序比较两个给定的数组。

Short description of Arrays.compare from the documentation: 文档中Arrays.compare简短描述:

If the two arrays share a common prefix then the lexicographic comparison is the result of comparing two elements, as if by Integer.compare(int, int), at an index within the respective arrays that is the prefix length. 如果两个数组共享一个公共前缀,则字典比较是比较两个元素的结果,就像通过Integer.compare(int,int)一样,在相应数组中作为前缀长度的索引处。 Otherwise, one array is a proper prefix of the other and, lexicographic comparison is the result of comparing the two array lengths. 否则,一个数组是另一个数组的正确前缀,并且词典比较是比较两个数组长度的结果。

Given you want to modify the source array then using Arrays.sort should suffice: 鉴于您想要修改 source数组,那么使用Arrays.sort应该足够了:

Arrays.sort(source, Arrays::compare); 

Given you want a new array as a result then I'd go the stream way: 鉴于你想要一个新的数组作为结果,那么我将采用流方式:

int[][] sorted = Arrays.stream(source)
                       .sorted(Arrays::compare)
                       .toArray(int[][]::new);

First sort each ArrayList in the Array.首先对数组中的每个 ArrayList 进行排序。

ArrayList<ArrayList<Integer>> allSubset = new ArrayList<>();

for(ArrayList<Integer> row : allSubset) {
     Collections.sort(row);
}

Second sort the whole ArrayList in lexicographically.其次按字典顺序对整个 ArrayList 进行排序。

allSubset.sort((ArrayList<Integer> o1, ArrayList<Integer> o2) -> {
            if(o2.size() == 0) return 1;
            int min = Math.min(o1.size(), o2.size());
            int i;
            for(i = 0; i < min - 1; i++) {
                if(o1.get(i).equals(o2.get(i))) continue;
                return o1.get(i).compareTo(o2.get(i));
            }
            return o1.get(i).compareTo(o2.get(i));
        });

or或者

    Collections.sort(allSubset, (ArrayList < Integer > first, ArrayList < Integer > second) -> {
        for (int i = 0; i < first.size() && i < second.size(); i++) {
            if (first.get(i) < second.get(i))
                return -1;
            if (first.get(i) > second.get(i))
                return 1;
        }
        if (first.size() > second.size())
            return 1;
        return -1;
    });

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

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