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

落爺英雄遲暮 提交于 2021-02-10 19:10:43

问题


I want to build a method in Java for sorting an array according to values in more than a given column. Let me explain that with an example (matrix array):

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

I need to sort every triplet according to the second position in decreasing order. After that, I need to sort the triplet in increasing order according to the third position.

The code that I'm using for that is:

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

The output of the previously described code is:

[[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]]

But the output needed must be:

[[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]]

Please note that according to the second position we have the following: 5,5,5,4,3,2,2,2,2,1 (decreasing order) and according to the third position the order is: 134,171,293 (for the triplets with a "5" in the second position), 191 (for the triplet with a "4" in the second position), 354 (for the triplet with a "3" in the second position), 432,456,475,491 (for the triplets with a "2" in the second position) and finally 282 for the triplet with a "1" in the second position.

Any help would be highly appreciated. Thanks.


回答1:


Remove the col parameter from the sortByColumn method since it is not really a parameter and change the method in this way:

// 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(). 
}

Of course change the call in main to sortbyColumn(matrix);

Explanation:

We need to compare by the third column only in cases of equality by the second column (which means thet first comparison numeric result is equal to 0). In that case we compare in reverse order, which we can obtain by multiplying the comparison result by -1.

Result:

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 



回答2:


Try this way:

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



回答3:


You can use the comparator chaining to sort first by one column in the specified order and then by another column in a different order:

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]

See also: How to use a secondary alphabetical sort on a string list of names?



来源:https://stackoverflow.com/questions/60689084/sorting-a-2d-array-by-values-in-more-than-one-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!