我是靠谱客的博主 饱满手机,这篇文章主要介绍java 中文字符串数组按照音序排列,现在分享给大家,希望可以做个参考。

复制代码 代码如下:

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

最后

以上就是饱满手机最近收集整理的关于java 中文字符串数组按照音序排列的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(211)

评论列表共有 0 条评论

立即
投稿
返回
顶部