1.对list进行排序,list中的参数类型是Stirng,参数的格式不完全一样,例如有null,"","51003","510020"等
public static void main(String args[]){
List<String> list = new ArrayList<>();
list.add("51003");
list.add("510020");
list.add("");
list.add(null);
list.add("510060");
Collections.sort(list,new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if(o1 == null || o2 == null){
return -1;
}
if(o1.length() > o2.length()){
return 1;
}
if(o1.length() < o2.length()){
return -1;
}
if(o1.compareTo(o2) > 0){
return 1;
}
if(o1.compareTo(o2) < 0){
return -1;
}
if(o1.compareTo(o2) == 0){
return 0;
}
return 0;
}
});
for(String s:list){
System.out.println(s);
}
}
输出:
null
51003
510020
510060