1

I have the below code

List<List1<String, String>> list1_a = Readafile(filename);

the data in the file is as follows

value 1    value 2        rank
      a    b              1
      a    c              2
      d    e              1
      d    c              4

I need to sort the list1_a according to the rank

i tried the below: Collections.sort(list1_a), but im no teven able to understand the error it is throwing me. Update: below is the error description:

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List<List1<String,String>>). The 
 inferred type List1<String,String> is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>
4

1 回答 1

4

这意味着仅Collections.sort(List<T>)接受T实现ComparatororComparable<T>的对象,而不是List1<String,String>(in List<List1<String,String>>, Tis a List1<String,String>)

您可以创建一个比较器List1<String,String>;(即实现的类Comparator<List1<String,String>>)像这样

public class YourComparator implements Comparator<List1<String,String>>{

  public int compare(List1<String,String> o1, List1<String,String> o2){
    //implement this method
  }

}

然后像这样对您的收藏进行排序:

Collections.sort(list1_a, new YourComparator());
于 2013-05-22T18:41:12.497 回答