如何在java中对特定的二维数组进行排序

我正在尝试对以下数组进行排序:


int hitlist[][] = new int [17][2];

排序信息总是在hitlist[i][0]并且它是数字的,但我找不到正确的方法Arrays.sort。


输入看起来像:


[0, 0] 

[4, 0] 

[3, 1] 

[4, 2] 

[4, 4] 

[5, 6] 

[4, 7] 

[4, 8] 

[1, 9] 

[4, 11] 

[4, 12] 

[2, 13] 

[4, 14] 

[4, 15] 

[0, 0] 

[0, 0] 

[0, 0] 

现在我希望它被排序为:


[1, 9]

[2, 13]

[3, 1]

[4, 0]

[4, 2] 

[4, 4]

[4, 7] 

[4, 8]

[4, 11] 

[4, 12]

[4, 14] 

[4, 15]


LEATH
浏览 185回答 4
4回答

至尊宝的传说

如果要根据索引对数组进行排序,可以Arrays::sort使用Comparator::comparingIntint index = 0;Arrays.sort(hitlist, Comparator.comparingInt(arr -> arr[index]));这是Ideone中的一个示例编辑根据您的评论和评论,您希望[0, 0]在排序后忽略数组中的 ,在这种情况下,您可以使用:int[][] hitlist = {{0, 0}, {4, 0}, {3, 1}, {4, 2}, {4, 4}, {5, 6}, {4, 7}, {4, 8}, {1, 9}, {4, 11}, {4, 12}, {2, 13}, {4, 14}, {4, 15}, {0, 0}, {0, 0}, {0, 0}};int index = 0;int[][] sortedArray = Arrays.stream(hitlist)        .filter(arr -> arr[0] != 0 && arr[1] != 0)        .sorted(Comparator.comparingInt(arr -> arr[index]))        .toArray(int[][]::new);Ideone 演示输出[1, 9][2, 13][3, 1][4, 2][4, 4][4, 7][4, 8][4, 11][4, 12][4, 14][4, 15][5, 6]

慕的地10843

虽然我更喜欢YCF_L 的解决方案,但这个实现使用了带有整数数组比较器的快速排序。这提供了更大的灵活性。import java.util.Arrays;/**&nbsp;* Based on Quicksort (right-most pivot) implementation from:&nbsp;&nbsp;&nbsp;* https://www.programcreek.com/2012/11/quicksort-array-in-java/&nbsp;*/public class Sorter {&nbsp; &nbsp; private static interface IntArrayComparator {&nbsp; &nbsp; &nbsp; &nbsp; int compare(int[] a, int[] b);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int hitlist[][] = new int[8][2];&nbsp; &nbsp; &nbsp; &nbsp; hitlist[4] = new int[] { 4, 10000 };&nbsp; &nbsp; &nbsp; &nbsp; hitlist[1] = new int[] { 1, 10 };&nbsp; &nbsp; &nbsp; &nbsp; hitlist[5] = new int[] { 5, 100000 };&nbsp; &nbsp; &nbsp; &nbsp; hitlist[0] = new int[] { 0, 1 };&nbsp; &nbsp; &nbsp; &nbsp; hitlist[2] = new int[] { 2, 100 };&nbsp; &nbsp; &nbsp; &nbsp; hitlist[7] = new int[] { 7, 10000000 };&nbsp; &nbsp; &nbsp; &nbsp; hitlist[3] = new int[] { 3, 1000 };&nbsp; &nbsp; &nbsp; &nbsp; hitlist[6] = new int[] { 6, 1000000 };&nbsp; &nbsp; &nbsp; &nbsp; quickSort(hitlist, (a, b) -> a[0] - b[0]);&nbsp; &nbsp; &nbsp; &nbsp; Arrays.asList(hitlist).stream().map(Arrays::toString).forEach(System.out::println);&nbsp; &nbsp; }&nbsp; &nbsp; public static void quickSort(int[][] arr, IntArrayComparator comparator) {&nbsp; &nbsp; &nbsp; &nbsp; quickSort(arr, comparator, 0, arr.length - 1);&nbsp; &nbsp; }&nbsp; &nbsp; public static void quickSort(int[][] arr, IntArrayComparator comparator, int start, int end) {&nbsp; &nbsp; &nbsp; &nbsp; int partition = partition(arr, comparator, start, end);&nbsp; &nbsp; &nbsp; &nbsp; if (partition - 1 > start) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quickSort(arr, comparator, start, partition - 1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (partition + 1 < end) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quickSort(arr, comparator, partition + 1, end);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static int partition(int[][] arr, IntArrayComparator comparator, int start, int end) {&nbsp; &nbsp; &nbsp; &nbsp; int[] pivot = arr[end];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = start; i < end; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (comparator.compare(arr[i], pivot) < 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] temp = arr[start];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[start] = arr[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[i] = temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; int[] temp = arr[start];&nbsp; &nbsp; &nbsp; &nbsp; arr[start] = pivot;&nbsp; &nbsp; &nbsp; &nbsp; arr[end] = temp;&nbsp; &nbsp; &nbsp; &nbsp; return start;&nbsp; &nbsp; }}结果[0, 1][1, 10][2, 100][3, 1000][4, 10000][5, 100000][6, 1000000][7, 10000000]

沧海一幻觉

这取决于您是要对行还是列进行排序。假设您想对每一行进行排序,您可以这样做。for(int i=0; i < hitlist.size(); i++ {&nbsp; &nbsp; &nbsp;Array.sort(hitlist[i]);}对列进行排序变得棘手,在这种情况下,您可以构造一个包含列值的新数组并将列排序或旋转为行(90 度),将其排序为行并再次旋转回来(-90 度)如果您需要其他任何东西,您必须自己实现搜索。希望这可以帮助

蛊毒传说

您可以将 int 数组装箱成 Integer 数组,然后在 lambda 函数中以数字方式比较两个 Integer 对象(第 0 个索引处的对象)。然后简单地将比较器传递给 Arrays.sort ,它将根据比较器引起的顺序对其进行排序。&nbsp; &nbsp; Integer[][] array= {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {1, 3},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {10, 5},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {4, 100},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {12, 30} };&nbsp; &nbsp; Comparator<Integer[]> arrayComparator = (a1, a2) -> a1[0].compareTo(a2[0]);&nbsp; &nbsp; Arrays.sort(array, arrayComparator);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java