Collections.sort 用法 比较器comparator
2014-07-18 16:55
302 查看
import java.util.Comparator; 首先构造一个比较器 public class SortList implements Comparator{//比较器 public int compare(Object o1,Object o2){ NewsValueContainer nvc1 = (NewsValueContainer)o1; NewsValueContainer nvc2 = (NewsValueContainer)o2; returnnvc2.getContainerTitle().compareTo(nvc1.getContainerTitle()); //return nvc1.getContainerTitle().compareTo(nvc2.getContainerTitle()); 则出来的结果的顺序与上面的相反 } } import java.util.Comparator; public class SortNewsValue implements Comparator{//必须要实现Comparator public int compare(Object o1,Object o2){//重写compare函数就行 NewsValue nv1 = (NewsValue)o1 ;//NewsValue是你要排序的实体类,javabean NewsValue nv2 = (NewsValue)o2 ; return nv2.getCreatedAt().compareTo(nv1.getCreatedAt());//getCreatedAt()就是获取排序比较的字段 } } import java.util.ArrayList; import java.util.Collections; import java.util.List; public class SortNVandNVC { public static void listSortNVandNVC(int tag,List<NewsValueContainer> listnvc){ SortNewsValue snv = new SortNewsValue() ; SortList sl = new SortList() ; if(0 == tag){//排getContainerTitle() Collections.sort(listnvc,sl);//第一个参数就是你的list,第二个参数就是你的比较器的对象 for(NewsValueContainer tmp:listnvc){ } }else{ for(int index = 0 ;index<listnvc.size();index++){ Collections.sort(listnvc.get(index).getNews(),snv); } } } }
相关文章推荐
- Collections.sort方法实现Comparator比较器进行二分查找
- Collections.sort(List<T> list, Comparator<? super T> c) 应用
- Collections.sort的两种用法
- java中list排序:Collections.sort() 排序函数的用法
- Collections.sort的两种用法
- java中的Comparator和Comparable比较器的用法
- Collections.sort()使用自定义比较器进行排序
- Java利用Comparator与Collections.sort对List排序
- Collections.sort的两种用法
- 使用Collections.sort(List<T>,Comparator<? super T>)给List<String>排序
- collections.sort(list, comparator) 详解
- 【Java】Collections中sort方法Comparator的重写
- Arrays.sort()和Collections.sort()的用法
- java数组排序Arrays.sort,以及Comparator接口的用法
- java集合排序——Collections.sort()的两种用法
- 集合中按照商品价格排序,按照学生编号排序,Collections集合里的sort方法,Comparator<T>接口,Comparable<T>接口,
- java中Collections.sort()函数的用法
- java.util.Collections.sort(List list)与Comparable,Comparator 接口
- Collections的自定义排序sort Comparator
- Collections.sort两种用法