对整数数组的ArrayList进行排序

1 java sorting arraylist

如何根据整数数组中的最后一个整数对整数数组的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]

Zon*_*ong 5

实现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)