繁体   English   中英

按多列中的值对二维数组进行排序

[英]Sorting a 2d array by values in more than one column

我想在 Java 中构建一个方法,用于根据多个给定列中的值对数组进行排序。 让我用一个例子来解释(矩阵数组):

int matrix[][] = {
        {0,2,432},{1,1,282},{2,2,456},{3,4,191},{4,5,293},
        {5,2,475},{6,2,491},{7,5,171},{8,5,134},{9,3,354}};

我需要按降序根据第二个位置对每个三元组进行排序。 之后,我需要根据第三个位置按升序对三元组进行排序。

我为此使用的代码是:

import java.util.*;

public class sort2DMatrixByColumn {
    // Function to sort by column
    public static void sortByColumn(int arr[][], int col) {
        // Using built-in sort function Arrays.sort
        Arrays.sort(arr, new Comparator<int[]>() {
            @Override
            // Compare values according to columns
            public int compare(final int[] entry1,
                               final int[] entry2) {

                if (entry1[col] < entry2[col])
                    return 1;
                else
                    return -1;
            }
        }); // End of function call sort().
    }

    // Driver Code
    public static void main(String args[]) {
        int matrix[][] = {
                {0,2,432},{1,1,282},{2,2,456},{3,4,191},{4,5,293},
                {5,2,475},{6,2,491},{7,5,171},{8,5,134},{9,3,354}};

        // Sort this matrix by 2rd Column
        int col = 2;
        sortByColumn(matrix, col - 1);

        // Display the sorted Matrix
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++)
                System.out.print(matrix[i][j] + " ");
            System.out.println();
        }
    }
}

前面描述的代码的输出是:

[[8,5,134],[7,5,171],[4,5,293],[3,4,191],[9,3,354],
 [6,2,491],[5,2,475],[2,2,456],[0,2,432],[1,1,282]]

但所需的输出必须是:

[[8,5,134],[7,5,171],[4,5,293],[3,4,191],[9,3,354],
 [0,2,432],[2,2,456],[5,2,475],[6,2,491],[1,1,282]]

请注意,根据第二个位置,我们有以下内容:5,5,5,4,3,2,2,2,2,1(递减顺序),根据第三个位置,顺序为:134,171,293(对于第二个位置为“5”的三元组)、191(第二个位置为“4”的三元组)、354(第二个位置为“3”的三元组)、432,456,475,491(第二个位置为“4”的三元组)第二个位置是“2”),最后是 282,第二个位置是“1”。

任何帮助将不胜感激。 谢谢。

试试这个方法:

int matrix[][] = {
        {0, 2, 432}, {1, 1, 282}, {2, 2, 456}, {3, 4, 191}, {4, 5, 293},
        {5, 2, 475}, {6, 2, 491}, {7, 5, 171}, {8, 5, 134}, {9, 3, 354}};

Comparator<int[]> secondDecrease = (a, b) -> b[1] - a[1];
Comparator<int[]> thirdIncrease = (a, b) -> a[2] - b[2];

Arrays.stream(matrix)
        .sorted(secondDecrease.thenComparing(thirdIncrease))
        .forEach(s -> System.out.println(Arrays.toString(s)));

从 sortByColumn 方法中删除 col 参数,因为它不是真正的参数,并以这种方式更改方法:

// Function to sort by column 
public static void sortbyColumn(int arr[][]) {
    // Using built-in sort function Arrays.sort 
    Arrays.sort(arr, new Comparator<int[]>() {
        @Override
        // Compare values according to columns 
        public int compare(final int[] entry1, final int[] entry2) {
            if (entry1[1] < entry2[1])
                return 1;
            else if (entry1[1] > entry2[1])
                return -1;

            return -1 * Integer.valueOf(entry2[2])
                    .compareTo(Integer.valueOf(entry1[2]));
        }
    }); // End of function call sort(). 
}

当然,将 main 中的调用更改为sortbyColumn(matrix);

解释:

我们只需要在第二列相等的情况下才需要通过第三列进行比较(这意味着第一个比较数字结果等于 0)。 在这种情况下,我们以相反的顺序进行比较,我们可以通过将比较结果乘以-1来获得。

结果:

8 5 134 
7 5 171 
4 5 293 
3 4 191 
9 3 354 
0 2 432 
2 2 456 
5 2 475 
6 2 491 
1 1 282 

您可以使用比较链接sort由一列指定的顺序,然后按不同的顺序的另一列第一:

int[][] matrix = {
        {0, 2, 432}, {1, 1, 282}, {2, 2, 456}, {3, 4, 191}, {4, 5, 293},
        {5, 2, 475}, {6, 2, 491}, {7, 5, 171}, {8, 5, 134}, {9, 3, 354}};
// sorting by second column in descending order,
// then by third column in ascending order
Arrays.sort(matrix, Comparator
        // <int[] - object type, Integer - return type>
        .<int[], Integer>comparing(arr -> arr[1], Comparator.reverseOrder())
        .thenComparing(arr -> arr[2], Comparator.naturalOrder()));
// output
Arrays.stream(matrix).map(Arrays::toString).forEach(System.out::println);
[8, 5, 134]
[7, 5, 171]
[4, 5, 293]
[3, 4, 191]
[9, 3, 354]
[0, 2, 432]
[2, 2, 456]
[5, 2, 475]
[6, 2, 491]
[1, 1, 282]

另请参阅:如何对名称字符串列表使用二级字母排序?

暂无
暂无

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

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