如何根据整数数组中的最后一个整数对整数数组的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});
Run Code Online (Sandbox Code Playgroud)
生成的ArrayList将包含:[0,0,0,1] [0,0,0,2] [0,0,0,3] [0,0,0,4]
实现Comparator和使用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]);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5834 次 |
| 最近记录: |