java 中文,数字,字母排序
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class TestUtil {
public static void main(String[] args) {
ArrayList<String> newArray= new ArrayList<String>();
newArray.add("汽车");
newArray.add("ab12");
newArray.add("ab21");
newArray.add("公安");
newArray.add("怡");
newArray.add("张新");
newArray.add("广州");
newArray.add("test");
newArray.add("pp");
newArray.add("8?23");
newArray.add("4.23");
newArray.add("北侧");
newArray.add("阿d雷");
newArray.add("而非");
newArray.add("猜测的");
newArray.add("飞的");
newArray.add("得出");
newArray.add("阿b当");
Collections.sort(newArray, (str1,str2)->{
char[] c={str1.toLowerCase().charAt(0),str2.toLowerCase().charAt(0)};//首字母
String[] str={str1.substring(0, 1),str2.substring(0, 1)};
int type[]={1,1};
for(int i=0;i<2;i++)
{
if(str[i].matches("[\\u4e00-\\u9fbb]+"))//中文字符
type[i]=1;
else if(c[i]>='a' && c[i]<='z')
type[i]=2;
else if(c[i]>='1' && c[i]<='9')
type[i]=3;
else
type[i]=4;
}
if(type[0]==1 && type[1]==1)
return Collator.getInstance(java.util.Locale.CHINESE).compare(str1, str2);
if(type[0]==type[1]) //同一类
return str1.compareTo(str2);
return type[1]-type[0]; //1和0互换可以调整显示顺序,str1, str2可以降序升序
});
newArray.forEach(c-> System.out.print(c+" "));
}
}