FWIW, the following code will sort an array of Chinese characters according Stroke:
import java.util.*;
import java.io.*;
import java.text.*;
public class CharSort implements Comparator{
java.text.RuleBasedCollator collator;
CharSort(){
collator=(RuleBasedCollator)java.text.Collator.getInstance(java.util.Locale.TRADITIONAL_CHINESE);
}
public void doSort(String[] str) throws java.io.IOException{
java.text.CollationKey[] keys=new java.text.CollationKey[str.length];
for(int i=0;i<keys.length;i++){
keys[i]=collator.getCollationKey(str[i]);
}
java.util.Arrays.sort(keys);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("cjk.txt"),"UTF8"));
for (int i=0;i<keys.length;i++){
bw.write(keys[i].getSourceString()+"\r\n");
}
bw.close();
}
public int compare(Object c1, Object c2) throws IllegalArgumentException{
if ((c1 instanceof CollationKey) && (c2 instanceof CollationKey)){
return collator.compare(((CollationKey)c1).getSourceString(),((CollationKey)c2).getSourceString());
} else throw new IllegalArgumentException();
}
public boolean equals(Object c1, Object c2){
if (this.compare(c1,c2)==0) return true;
else return false;
}
public static void main(String[] args) throws java.lang.Exception{
CharSort chSort=new CharSort();
String[] str={"\u81f3",
"\u6f22",
"\u8a31",
"\u6c0f",
"\u59cb",
"\u6709",
"\u8aaa",
"\u6587",
"\u7136",
"\u91cd",
"\u7fa9",
"\u800c",
"\u7565",
"\u65bc",
"\u97f3"};
chSort.doSort(str);
}
}
The code shown above is modified from this post: http://forum.java.sun.com/thread.jspa?forumID=16&threadID=369970
What I need now is the code to sort by Phonetic.
V.V.>
ok..., I got it all figured out. To sort phonetically, replace the following line of code:
collator=(RuleBasedCollator)java.text.Collator.getInstance(java.util.Locale.TRADITIONAL_CHINESE);
with:
Locale loc=new Locale("zh","CN");
collator=(RuleBasedCollator)Collator.getInstance(loc);
V.V.
Message was edited by: V.V.
viravan