当前位置:主页 > 查看内容

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

发布时间:2021-07-16 00:00| 位朋友查看

简介:复制代码 代码如下: 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……
复制代码 代码如下:

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

本文转载自网络,原文链接:https://m.jb51.net/article/16561.htm
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:JAVA 18位身份证号码校验码的算法 下一篇:没有了

推荐图文


随机推荐