【发布时间】:2013-11-29 22:47:20
【问题描述】:
为了解决一个问题,我需要对二维数组的每一行进行排序。
初始数组
35 22 42 28 20 11
35 21 14 33 1 34
35 42 16 22 38 8
29 12 42 25 28 2
25 28 22 11 20 35
1 40 41 43 44 45
排序后:
11 20 22 28 35 42
1 14 21 33 34 35
8 16 22 35 38 42
2 12 25 28 29 42
11 20 22 25 28 35
1 40 41 43 44 45
已关注此文档
Sorting a 2D Integer array based on a column
都推荐给use Comparator.
But i got an impression that it compares its two arguments. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
So couldn't really make it work for my case (6 columns).
任何想法/指针都会有所帮助。
接受输入后,这是我尝试过的
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] int1, int[] int2) {
return Integer.valueOf(int1[0]).compareTo(int2[0]);
}
});
for(c=0;c<6;c++) {
for(d=0;d<6;d++) {
System.out.println(arr[c][d]);
}
System.out.println();
}
得到这个..
35 22 42 28 20 11
35 21 14 33 1 34
35 42 16 22 38 8
29 12 42 25 28 2
25 28 22 11 20 35
1 40 41 43 44 45
1
40
41
43
44
45
25
28
22
11
20
35
29
12
42
25
28
2
35
22
42
28
20
11
35
21
14
33
1
34
35
42
16
22
38
8
【问题讨论】:
-
您是否需要就地执行此操作(作为问题或作业),还是您在访问任意数据结构时遇到的实际问题?