简体   繁体   English

按升序对二维数组进行排序

[英]Sorting a 2d array in ascending order

I'm trying to sort this 2d array in ascending order from {4,2},{1,7},{4,5},{1,2},{1,1},{4,1} to this {1,1},{1,2},{1,7},{4,1},{4,2},{4,5} .我试图从{4,2},{1,7},{4,5},{1,2},{1,1},{4,1}到这个以升序对这个二维数组进行排序{1,1},{1,2},{1,7},{4,1},{4,2},{4,5} . But for some reason it still gives me the original array and doesnt sort.但出于某种原因,它仍然给了我原始数组并且没有排序。 What am I missing?我错过了什么?

public class Sort {
    public static void main(String[] args) {
        int[][] array = {{4, 2}, {1, 7}, {4, 5}, {1, 2}, {1, 1}, {4, 1}};
        sort(array);
        print(array);
    }

    //Row Length
    public static void print(int[][] m) {
        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++) {
                System.out.print("{" + m[i][j] + "}" + " ");
            }
            System.out.println();
        }
    }

    public static int sort(int A[][]) {
        int newArray = 0;
        for (int i = 0; i < A.length; i++) {
            for (int j = i + 1; j < A[i].length - 1; j++) {
                if (A[j][0] > A[j + 1][0]) {
                    int temp = 0;
                    temp = A[i][j];
                    A[i][j] = A[i + 1][j + 1];
                    A[i + 1][j + 1] = A[i][j];

                    System.out.print(" " + A[i][j]);
                }
            }
        }
        return newArray;
    }
}

This does what you want:这做你想要的:

  • Your if statement never executed, because your logic was wrong when doing comparisons.您的 if 语句从未执行过,因为在进行比较时您的逻辑是错误的。

  • Consequently temp never ever got set in your original code.因此,您的原始代码中从未设置过 temp。

  • To top that off, temp should have been an int[] , not an int .最重要的是, temp应该是一个int[] ,而不是一个int

  • There was also no need to return an int from the sort method.也不需要从 sort 方法返回int

  • I also slightly tweaked your print routine.我还稍微调整了您的打印程序。


import static java.lang.System.out;
public class Playground {
    public static void main(String[] args) {
        int[][] array = {{4, 2}, {1, 7}, {4, 5}, {1, 2}, {1, 1}, {4, 1}};
        sort(array);
        print(array);
    }

    public static void print(int[][] m) {
        for (int i = 0; i < m.length; i++) {
            if (i > 0) {
                out.print(",");
            }
            out.print("{" + m[i][0] + "," + m[i][1] + "}");
        }
        out.println();
    }

    public static void sort(int A[][]) {
        boolean unsorted = true;
        while (unsorted) {
            unsorted = false;
            for (int i = 0; i < A.length - 1; i++) {
                if ((A[i][0] > A[i + 1][0])
                        || ((A[i][0] == A[i + 1][0])
                        && (A[i][1] > A[i + 1][1])
                )) {
                    int[] temp = new int[2];
                    temp[0] = A[i][0];
                    temp[1] = A[i][1];
                    A[i][0] = A[i + 1][0];
                    A[i][1] = A[i + 1][1];
                    A[i + 1] = temp;
                    unsorted = true;
                }
            }
        }
    }
}

You can see it running here .你可以看到它在这里运行。

You can use the comparator chaining to sort first by one column and then by another column:您可以使用比较器链接首先按一列sort ,然后按另一列sort

int[][] array = {{4, 2}, {1, 7}, {4, 5}, {1, 2}, {1, 1}, {4, 1}};

Arrays.sort(array, Comparator
        .<int[]>comparingInt(arr -> arr[0])
        .thenComparing(arr -> arr[1]));

System.out.println(Arrays.deepToString(array));
// [[1, 1], [1, 2], [1, 7], [4, 1], [4, 2], [4, 5]]

See also: Sorting a 2d array by values in more than one column另请参阅:按多列中的值对二维数组进行排序

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

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