【问题标题】:Sorting a 2d array in ascending order按升序对二维数组进行排序
【发布时间】:2020-02-01 17:42:03
【问题描述】:

我正在尝试按从{4,2},{1,7},{4,5},{1,2},{1,1},{4,1} 到这个{1,1},{1,2},{1,7},{4,1},{4,2},{4,5} 的升序对这个二维数组进行排序。但由于某种原因,它仍然给了我原始数组并且没有排序。我错过了什么?

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

【问题讨论】:

    标签: java arrays sorting multidimensional-array


    【解决方案1】:

    这就是你想要的:

    • 你的 if 语句从未执行,因为你的逻辑是错误的 做比较。

    • 因此,您的原始代码中从未设置过 temp。

    • 最重要的是,temp 应该是 int[],而不是 int

    • 也不需要从 sort 方法返回 int

    • 我还稍微调整了您的打印程序。


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

    你可以看到它正在运行here

    【讨论】:

      【解决方案2】:

      您可以先使用 比较器链接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]]
      

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

      【讨论】:

        猜你喜欢
        • 2021-05-23
        • 2013-08-17
        • 2021-04-11
        • 1970-01-01
        • 2020-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多