源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-07-30 11:20 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:java 中文字符串数组按照音序排列
[u]复制代码[/u] 代码如下:
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; } } }
调用:
[u]复制代码[/u] 代码如下:
String[] str = {"北京","中国","亚运会"}; Arrays.sort(str,new SortComparator()); for(int len=0;len<str.length;len++){ System.out.println(str[len]); }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部