我想对整数数组列表的数组列表进行排序,我需要帮助吗?
我被告知我需要实现 comparator 或 comparable 然后使用 collection.sort 来按顺序对列表的列表进行排序...
ArrayList<ArrayList<Integer>> g = new ArrayList<ArrayList<Integer>>()
If you look at the list of list as the following example:
C1 – 5,4,10
C2 – 3,2,1
C3 – 7,8,6
First it will be sorted like this:
C1 – 4,5,10
C2 – 1,2,3
C3 – 6,7,8
Then it will be sorted like this
C1 – 1,2,3
C2 – 4,5,6
C3 – 7,8,10
最佳答案
没有对空列表进行错误检查,但在这里。
List<List<Integer>> list = Arrays.asList(Arrays.asList(10, 5, 4),
Arrays.asList(3, 2, 1), Arrays.asList(7, 8, 6));
for (List<Integer> l : list) {
Collections.sort(l);
}
Collections.sort(list, new Comparator<List<Integer>>() {
public int compare(List<Integer> o1, List<Integer> o2) {
return o1.get(0).compareTo(o2.get(0));
}
});
System.out.println(list);
在 Java 8 中,它变得更加简洁:
List<List<Integer>> list = Arrays.asList(Arrays.asList(10, 5, 4),
Arrays.asList(3, 2, 1), Arrays.asList(7, 8, 6));
list.forEach(Collections::sort);
Collections.sort(list, (l1, l2) -> l1.get(0).compareTo(l2.get(0)));
System.out.println(list);
关于java - 对整数数组列表的数组列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10794148/