列表排序 --- Collections.sort()

Collections.sort()能够实现对List等Collection集合排序

Collections.sort() 默认可以对List<String> 和 List<Integer>进行排序,不用实现comparator或者comparable接口

             重要: JAVA中 Integer对象 int数据 可以隐式转换                   

Collections.sort(List<Type> list)

这种情况要求Type类型自身实现Comparable接口(类中覆盖compareTo方法),如下所示:

或者下面这种情况

 

Collections.sort(List<Type> list,new SortComparator());

   这种情况下无需再Type上实现comparable接口,需要单独实现Comparator接口(实现类中实现compare方法),并调用

或者下面这种

 

 

posted @ 2016-04-17 19:46  窗外是绿色的  阅读(277)  评论(0编辑  收藏  举报