如何对List类型列表进行排序?

0 java

List<List> d=new Arraylist<List>();
Run Code Online (Sandbox Code Playgroud)

我该如何对列表进行排序d?如果我使用Collection.sort(d);我收到以下错误:

Bound mismatch: The generic method sort(List<T>) of type Collections is not
applicable for the arguments (List<List>). The inferred type List is not a
valid substitute for the bounded parameter <T extends Comparable<? super T>>
Run Code Online (Sandbox Code Playgroud)

RNJ*_*RNJ 5

你有一份清单.内部列表无法排序为Collections.sort不知道如何.如果你愿意,你可以这样做

Collections.sort (d, new Comparator(){...});
Run Code Online (Sandbox Code Playgroud)

然后传入的比较器将确定如何对列表进行排序.

例如,比较器可以假定

new Comparator<List>(){
       public int compare(List l1,List l2){
            //write appropriate checks and comparisons here
            return l1.get(0).compareTo(l2.get(0);
       }}
Run Code Online (Sandbox Code Playgroud)

我想你应该考虑一下你想做什么.如果你有一个列表然后每个元素本身就是一个列表你真的想要对它进行排序吗?按什么排序?

  • 我认为列表长度是一个很好的候选人.此外,词典顺序将是一个有用的选择(按第一个元素排序,打破与第二个元素的联系,等等). (2认同)