【问题标题】:Sorting a 2D array of integers by values in columns按列中的值对二维整数数组进行排序
【发布时间】:2023-03-26 00:37:01
【问题描述】:

我正在尝试根据每列的值按升序对 Java 中的整数 2D array 进行排序。

让我用下面的例子来解释我的目标:

这是我的数组

int[][] array = new int[][]{
        {7, 3, 9},
        {9, 1, 3},
        {5, 8, 8}};

这是预期的数组

int[][] newArray = new int[][]{
        {5, 1, 3},
        {7, 3, 8},
        {9, 8, 9}};

从示例中可以看出,newArray 上的每个值都与 array 相同,但现在在每列中按升序排列。

论坛上几乎所有的问题都集中在如何根据行或列的值对二维数组进行排序,但我对每一列都需要这个。

【问题讨论】:

    标签: java arrays sorting multidimensional-array columnsorting


    【解决方案1】:

    你可以这样做。

    • 静态 Lambda 按列排序。我这样做是为了绕过对修改流内部局部变量的有效最终限制,在本例中为列。
    • sortByColumn 方法为每个列数调用此 lambda。
    • 这仅支持矩形矩阵。
    static BiFunction<int[][], Integer, int[][]> sortColumn = (arr,c) -> {
         int[] temp = IntStream.range(0, arr.length)
            .map(i -> arr[i][c]).sorted().toArray();
         for (int i = 0; i < arr.length; i++) {
             arr[i][c] = temp[i];
         }
         return arr;
    };
        
    public static void main(String[] args) {
        int[][] array =
                new int[][] { { 7, 3, 9 }, { 9, 1, 3 }, { 5, 8, 8 } };
        
        array = sortByColumn(array);
        System.out.println(Arrays.deepToString(array)); 
    }
    

    打印

    [[5, 1, 3], [7, 3, 8], [9, 8, 9]]
    
        
    public static int[][] sortByColumn(int[][] arr) {
         for (int col = 0; col < arr[0].length; col++) {
             arr = sortColumn.apply(arr,col);
         }
         return arr;
    }
    

    【讨论】:

      【解决方案2】:

      要对矩阵的列元素进行排序,可以对转置矩阵的行元素进行排序,然后将其转回:

      int m = 3;
      int n = 4;
      int[][] arr = {
              {7, 3, 9, 2},
              {9, 1, 3, 1},
              {5, 8, 8, 7}};
      
      // sorting transposed matrix
      int[][] arr2 = IntStream
              // iterate over the indices
              // of the rows of the matrix
              .range(0, n)
              .mapToObj(i -> IntStream
                      // iterate over the
                      // indices of the columns
                      .range(0, m)
                      .map(j -> arr[j][i])
                      .sorted()
                      .toArray())
              .toArray(int[][]::new);
      
      // transposing sorted matrix
      int[][] arr3 = IntStream
              // iterate over the indices of the
              // rows of the transposed matrix
              .range(0, m)
              .mapToObj(i -> IntStream
                      // iterate over the
                      // indices of the columns
                      .range(0, n)
                      .map(j -> arr2[j][i])
                      .toArray())
              .toArray(int[][]::new);
      
      // output
      Arrays.stream(arr3).map(Arrays::toString).forEach(System.out::println);
      
      [5, 1, 3, 1]
      [7, 3, 8, 2]
      [9, 8, 9, 7]
      

      另见:Sorting 2D array of integers by column

      【讨论】:

        猜你喜欢
        • 2021-02-03
        • 1970-01-01
        • 2020-06-26
        • 2013-08-17
        • 2013-04-12
        • 2022-01-01
        • 2021-05-23
        • 2011-12-17
        • 2011-10-25
        相关资源
        最近更新 更多