3种排序
TreeSet <T>: 排序:必须在对象所属的类<T>中实现java.lang.Comparable接口,并且重写toCompare()方法。
List <T>+ Collection工具类:
- Collections.sort(list):需要实现Comparable接口,并且重写toCompare()方法。
- Collections.sort(list,new Comparator(){@Override public int compare(Object o01, Object o02)...}); 对象类不用实现接口。
2种逆序输出方法
Collections工具逆序输出元素:
- Collections.sort(list, Collections.reverseOrder()); // 降序排列;
- Comparator<Person> descComparator = Collections.reverseOrder(ascComparator);
// 从升序排序对象产生一个反转(降序)的排序对象
其它一些常用的api函数:
Collections.reverse(list);反转排序。
String属性排序可以用以下的方法
public
int compareTo(Cat o) {return this.getName().compareTo(o.getName());}
comparetTo()
String属性排序采用 Collections.sort(list,
String.CASE_INSENSITIVE_ORDER);
java 集合排序问题
原文:http://blog.csdn.net/langjian2012/article/details/44812237