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
版权声明:如需转载,请注明!PS:如是转载随便,请忽略