如何根据整数数组中的最后一个整数对整数数组的 ArrayList 进行排序?
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});
生成的 ArrayList 将包含:[0,0,0,1] [0,0,0,2] [0,0,0,3] [0,0,0,4]
最佳答案
实现一个比较器
并使用Collections.sort
。或者同时进行:
Collections.sort(paths, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return (Integer)(a[a.length-1]).compareTo(b[b.length-1]);
}
});
关于java - 对整数数组的 ArrayList 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19596950/