Java,集合按自定义规则排序

 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test{

public static void main(String[] args){

  List<String> list = new ArrayList<>();
  list.add("20141208000000");
  list.add("20151130000000");
  list.add("19900317000000");
  list.add("20160726172307");
  list.add(null);
  Collections.sort(list,new Comparator<Object>() {
    @Override
    public int compare(Object obj1,Object obj2){
      if(obj1==null||"".equals(obj1)){
        return 1;
      }
      if(obj2==null||"".equals(obj2)){
        return 1;
      }
      return new Double((String)obj2).compareTo(new Double((String) obj1));
    }
  });
  for(String item : list){
    System.out.println(item);
  }
 }
}

 


结果:
20160726172307
20151130000000
20141208000000
19900317000000
null

posted @ 2017-07-17 16:53  紫薇帝星的故事  阅读(2014)  评论(0编辑  收藏  举报