[英]Sorting a 2d array by values in more than one column
我想在 Java 中構建一個方法,用於根據多個給定列中的值對數組進行排序。 讓我用一個例子來解釋(矩陣數組):
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}};
我需要按降序根據第二個位置對每個三元組進行排序。 之后,我需要根據第三個位置按升序對三元組進行排序。
我為此使用的代碼是:
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]]
但所需的輸出必須是:
[[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(遞減順序),根據第三個位置,順序為:134,171,293(對於第二個位置為“5”的三元組)、191(第二個位置為“4”的三元組)、354(第二個位置為“3”的三元組)、432,456,475,491(第二個位置為“4”的三元組)第二個位置是“2”),最后是 282,第二個位置是“1”。
任何幫助將不勝感激。 謝謝。
試試這個方法:
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}};
Comparator<int[]> secondDecrease = (a, b) -> b[1] - a[1];
Comparator<int[]> thirdIncrease = (a, b) -> a[2] - b[2];
Arrays.stream(matrix)
.sorted(secondDecrease.thenComparing(thirdIncrease))
.forEach(s -> System.out.println(Arrays.toString(s)));
從 sortByColumn 方法中刪除 col 參數,因為它不是真正的參數,並以這種方式更改方法:
// 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);
解釋:
我們只需要在第二列相等的情況下才需要通過第三列進行比較(這意味着第一個比較數字結果等於 0)。 在這種情況下,我們以相反的順序進行比較,我們可以通過將比較結果乘以-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
您可以使用比較鏈接到sort
由一列指定的順序,然后按不同的順序的另一列第一:
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}};
// sorting by second column in descending order,
// then by third column in ascending order
Arrays.sort(matrix, Comparator
// <int[] - object type, Integer - return type>
.<int[], Integer>comparing(arr -> arr[1], Comparator.reverseOrder())
.thenComparing(arr -> arr[2], Comparator.naturalOrder()));
// output
Arrays.stream(matrix).map(Arrays::toString).forEach(System.out::println);
[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]
另請參閱:如何對名稱字符串列表使用二級字母排序?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.