问题描述
我想用Java构建一种方法,以根据给定列中的更多值对数组进行排序.让我用一个示例(矩阵数组)进行解释:
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.
我为此使用的代码是:
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();
}
}
}
上述代码的输出为:
[[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]]
请注意,根据第二个位置,我们有以下内容:5,5,5,4,3,2,2,2,2,1,1(递减顺序),根据第三个位置,顺序是:134,171,293(对于在第二位置具有"5"的三胞胎),191(对于在第二位置具有"4"的三胞胎),354(对于在第二位置具有"3"的三胞胎),432,456,475,491(对于在第二位置具有"2"的三元组)和最后对于具有"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.
推荐答案
从sortByColumn方法中删除col参数,因为它并不是真正的参数,并以这种方式更改方法:
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().
}
当然可以将main中的调用更改为 sortbyColumn(matrix);
Of course change the call in main to sortbyColumn(matrix);
说明:
仅在与第二列相等的情况下才需要在第三列进行比较(这意味着第一个比较数值结果等于0).在这种情况下,我们以相反的顺序进行比较,可以通过将比较结果乘以 -1
来获得.
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
.
结果:
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
这篇关于通过多列中的值对2d数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!