对一个包含整数的二维数组按列进行排序。

huangapple go评论84阅读模式
英文:

Sorting a 2D array of integers by values in columns

问题

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

让我用以下示例解释我的目标:

这是我的数组

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相同,但现在按每列升序排序。

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

英文:

I am trying to sort a 2D array of integers in Java in increasing order according to the values of every column.

Let me explain my objective with the following example:

This is my array:

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

Here is the expected array:

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

As can see in the example, every values on newArray are the same as array but now ordered in each column in increasing order.

Almost all the questions in the forum are focused on how to sort a 2D array according to the values of a row or column, but I need this for every column.

答案1

得分: 1

以下是翻译后的内容:

你可以像这样做。

  • 静态 Lambda 函数按列进行排序。我这样做是为了绕过流内修改局部变量的有效 final 限制,这种情况下是列。
  • 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;
}
英文:

You could do it like this.

  • The static Lambda does the sort by column. I did this to get around the effective final restriction on modifying local variables inside of streams, in this case the column.
  • The sortByColumn method calls this lambda for each number of columns.
  • This only supports rectangular matrices.
static BiFunction&lt;int[][], Integer, int[][]&gt; sortColumn = (arr,c) -&gt; {
	 int[] temp = IntStream.range(0, arr.length)
		.map(i -&gt; arr[i][c]).sorted().toArray();
     for (int i = 0; i &lt; 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));	
}

Prints

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

</details>



# 答案2
**得分**: 0

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

```java
int m = 3;
int n = 4;
int[][] arr = {
        {7, 3, 9, 2},
        {9, 1, 3, 1},
        {5, 8, 8, 7}};
// 对转置矩阵进行排序
int[][] arr2 = IntStream
        // 遍历矩阵的行索引
        .range(0, n)
        .mapToObj(i -> IntStream
                // 遍历矩阵的列索引
                .range(0, m)
                .map(j -> arr[j][i])
                .sorted()
                .toArray())
        .toArray(int[][]::new);
// 转置已排序的矩阵
int[][] arr3 = IntStream
        // 遍历转置矩阵的行索引
        .range(0, m)
        .mapToObj(i -> IntStream
                // 遍历转置矩阵的列索引
                .range(0, n)
                .map(j -> arr2[j][i])
                .toArray())
        .toArray(int[][]::new);
// 输出结果
Arrays.stream(arr3).map(Arrays::toString).forEach(System.out::println);
[5, 1, 3, 1]
[7, 3, 8, 2]
[9, 8, 9, 7]

另请参阅:按列对整数的二维数组进行排序

英文:

To sort the elements of the columns of a matrix, you can sort the elements of the rows of the transposed matrix and then transpose it back:

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 -&gt; IntStream
                // iterate over the
                // indices of the columns
                .range(0, m)
                .map(j -&gt; 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 -&gt; IntStream
                // iterate over the
                // indices of the columns
                .range(0, n)
                .map(j -&gt; 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]

<sup>See also: Sorting 2D array of integers by column</sup>

huangapple
  • 本文由 发表于 2020年10月21日 07:05:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64454498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定