public class ListSort {

public static void main(String[] args) {
ArrayList test = new ArrayList();
test.add(new Integer(5));
test.add(new Integer(3));
test.add(new Integer(7));
test.add(new Integer(1));
test.add(new Integer(5));
System.out.println(test);
Collections.sort(test);
System.out.println(test);
Comparator c = new Comparator() {
public boolean equals(Object obj) {
return false;
} public int compare(Object o1, Object o2) {
Integer i = (Integer)o1;
Integer j = (Integer)o2;
if(i.intValue()<j.intValue()){
return 1;
}else if(i.intValue()==j.intValue()){
return 0;
}
return -1;
}
};
Collections.sort(test,c);
System.out.println(test);
}
}

解决方案 »

  1.   

    麻烦问一下 Collections.sort(List list, Comparator c)和Collections.sort()的区别,再次谢谢!!!!!
      

  2.   

    A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as TreeSet or TreeMap).多看看帮助文档。
      

  3.   

    List list = ...;
    Comparator comp = Collections.reverseOrder();
    Collections.sort(list, comp);