欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 中文字符串?dāng)?shù)組按照音序排列

 更新時(shí)間:2008年11月21日 20:03:25   作者:  
非常不錯(cuò)的中文字符串?dāng)?shù)組按照音序
復(fù)制代碼 代碼如下:

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

調(diào)用:
復(fù)制代碼 代碼如下:

String[] str = {"北京","中國(guó)","亞運(yùn)會(huì)"};
Arrays.sort(str,new SortComparator());
for(int len=0;len<str.length;len++){
System.out.println(str[len]);
}

相關(guān)文章

最新評(píng)論