자바 중국어 문자열 배열


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

좋은 웹페이지 즐겨찾기