[英]Sorting arraylist of array list
I waned to sort an array list of arraylist of numbers using java, so basically if I have the following arraylist:我想使用 java 对 arraylist 的数组列表进行排序,所以基本上如果我有以下 arraylist:
arr.add(<1,0>)
arr.add(<0,3>)
arr.add(<2,1>)
arr.add(<2,2>)
the output should be: output 应该是:
<0,3>
<1,0>
<2,1>
<2,2>
The arraylist should be sorted according to the first key then the second key. arraylist 应该按照第一个键然后第二个键排序。
Here is one way.这是一种方法。 Use
lists.sort()
使用
lists.sort()
The data数据
List<List<Integer>> lists =
new ArrayList<>(List.of(List.of(0, 3), List.of(1, 0),
List.of(2, 1), List.of(2, 2)));
Shuffle for demo随机播放演示
Collections.shuffle(lists);
lists.sort(Comparator
.comparing((List<Integer> lst) -> lst.get(0))
.thenComparing(lst -> lst.get(1)));
lists.forEach(System.out::println);
prints印刷
[0, 3]
[1, 0]
[2, 1]
[2, 2]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.