这是一个带有不进行自动装箱或强制转换的比较器的版本:
public class Sorter {
public static void main(String[] args) {
ArrayList<int[]> paths = new ArrayList<int[]>();
paths.add(new int[] { 0, 0, 0, 0, 4 });
paths.add(new int[] { 0, 0, 0, 0, 2 });
paths.add(new int[] { 0, 0, 0, 0, 1 });
paths.add(new int[] { 0, 0, 0, 0, 3 });
Collections.sort(paths, new Comparator<int[]>() {
private static final int INDEX = 4;
@Override
public int compare(int[] o1, int[] o2) {
return Integer.compare(o1[INDEX], o2[INDEX]);
}
});
for (int[] is : paths) {
System.out.println(Arrays.toString(is));
}
}
}
将导致:
[0, 0, 0, 0, 1]
[0, 0, 0, 0, 2]
[0, 0, 0, 0, 3]
[0, 0, 0, 0, 4]