java 中文字符串数组按照音序排列
java 中文字符串数组按照音序排列
发布时间:2016-12-29 来源:查字典编辑
摘要:复制代码代码如下:publicclassSortComparatorimplementsComparator{publicintcompar...

复制代码 代码如下:

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

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新JSP教程学习
热门JSP教程学习
编程开发子分类