【发布时间】:2021-12-15 02:10:17
【问题描述】:
我有一个数组列表,类似于List<List<Integer>> res = new ArrayList();
经过一些处理后,我的 res 数组列表将包含
[1, 1, 2]
[1, 1]
[1, 2]
[1]
[2]
这些元素,但我想要排序的顺序,看起来像
[1]
[1 1]
[1 1 2]
[1 2]
[2]
所以我所做的是
Collections.sort(res,new Comparator<List<Integer>>(){
public int compare(List<Integer> o,List<Integer> s){
int c=0;
//c=o.size().compareTo(s.size());
//if(c==0){
for(int i=0;i<Math.min(s.size(),o.size());i++){
c=o.get(i).compareTo(s.get(i));
if(c!=0) return c;
}
//}
return c;
}
});
但它不起作用
【问题讨论】:
-
在
compare方法中放一个断点,看看一些比较是什么样的。或者只是在纸上计算 [1] 和 [1 2] 之间的比较结果。
标签: java sorting arraylist collections