您的位置:首页 > 其它

【算法】实现对中文字符串数组按照音序排列

2008-11-21 18:27 615 查看
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]);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐