当前位置: 移动技术网 > IT编程>开发语言>Java > java 中文字符串数组按照音序排列

java 中文字符串数组按照音序排列

2017年12月12日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

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]);
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网