/**
* 字符串排序
* 首先根据字母排序,然后再根据数字排序
*
*list 需要排序的集合
*key 指定排序的key
*调用该方法,无需返回值
*/
public static void sortString( List<Map<String, Object>> list,String key) {
Collections.sort(list, (o1, o2) -> {
String str1 = o1.get(key)+"";
String str2 = o2.get(key)+"";
if(str1==null && str2==null){
return 0;
}
if(str1==null){
return 1;
}
if(str2==null){
return 1;
}
char c1;
char c2;
for(int i = 0; i < str1.length() ; i++) {
c1 = str1.charAt(i);
try {
c2 = str2.charAt(i);
} catch (Exception e) {
return 1;
}
if(Character.isDigit(c1) && Character.isDigit(c2)) {
String numStr1="";
String numStr2="";
for(int j = i; j < str1.length(); j++) {
c1 = str1.charAt(j);
if(!Character.isDigit(c1)) {
break;
}
numStr1+=c1;
}
for(int j = i; j <str2.length(); j++) {
c2 = str2.charAt(j);
if(!Character.isDigit(c2)) {
break;
}
numStr2+=c2;
}
return Integer.valueOf(numStr1).compareTo(Integer.valueOf(numStr2));
}
if(c1!=c2){
return c1-c2;
}
}
return 0;
});
}
排序-字母数组(首先根据字母排序,然后再根据数字排序)
最新推荐文章于 2023-07-07 23:30:18 发布