java 中文字符串数组按照音序排列
程序员文章站
2023-12-04 10:16:34
复制代码 代码如下:public class sortcomparator implements comparator{ public int compare(object...
复制代码 代码如下:
public class sortcomparator implements comparator{
public int compare(object o1,object o2) {
try{
byte[] buf1 = ((string) o1).getbytes("unicode");
byte[] buf2 = ((string) o2).getbytes("unicode");
int size = math.min(buf1.length, buf2.length);
for (int i = 0; i < size; i++) {
if (buf1[i] < buf2[i])
return -1;
else if (buf1[i] > buf2[i])
return 1;
}
return buf1.length - buf2.length;
}catch(unsupportedencodingexception ex) {
return 0;
}
}
}
调用:
复制代码 代码如下:
string[] str = {"北京","中国","亚运会"};
arrays.sort(str,new sortcomparator());
for(int len=0;len<str.length;len++){
system.out.println(str[len]);
}
上一篇: 联想笔记本电池怎么升级微码?