I have a list of String which each of them maybe consists of Chinese character or number or English Character. (For example: "z莉z莉", "露西", "蒋豪", "qwer", "zout1iao", "hah"). What i want do is sort them in English alphabet order like Android's Contacts app.(That is "hah", "蒋豪", "露西", "qwer", "z莉z莉", "zout1iao") So my strategy is converting all Chinese character into pinyin, then comparing them as usual. I have tried library yinpin4j, it works well on J2SE platform, but when I run the same code on Android, it raised an error. the java code is:
@Override
public int compareTo(People another) {
String onePinyin = HanziHelper.words2Pinyin(name);
String theOtherPinyin = HanziHelper.words2Pinyin(another.getName());
return onePinyin.compareTo(theOtherPinyin);
}
private static String char2String(char c) {
StringBuilder sb = new StringBuilder();
return sb.append(c).toString();
}
public static String char2Pinyin(char c) {
String[] pinyin = null;
try {
pinyin = PinyinHelper.toHanyuPinyinStringArray(c, format);
} catch(BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
if(pinyin == null) {
return char2String(c);
} else {
return pinyin[0];
}
}
public static String words2Pinyin(String words) {
StringBuilder sb = new StringBuilder();
char[] chars = words.toCharArray();
for(int i = 0, length = chars.length; i < length; i++) {
sb.append(char2Pinyin(chars[i]));
}
return sb.toString();
}
error is:
04-15 12:37:15.750: W/System.err(6898): java.io.IOException: BufferedInputStream is closed
04-15 12:37:15.791: W/System.err(6898): at java.io.BufferedInputStream.streamClosed(BufferedInputStream.java:116)
04-15 12:37:15.791: W/System.err(6898): at java.io.BufferedInputStream.read(BufferedInputStream.java:294)
04-15 12:37:15.791: W/System.err(6898): at java.io.InputStreamReader.read(InputStreamReader.java:255)
04-15 12:37:15.791: W/System.err(6898): at java.io.BufferedReader.fillBuf(BufferedReader.java:128)
04-15 12:37:15.791: W/System.err(6898): at java.io.BufferedReader.read(BufferedReader.java:236)
04-15 12:37:15.791: W/System.err(6898): at java.util.Properties.load(Properties.java:307)
04-15 12:37:15.791: W/System.err(6898): at java.util.Properties.load(Properties.java:266)
04-15 12:37:15.791: W/System.err(6898): at net.sourceforge.pinyin4j.ChineseToPinyinResource.initializeResource(Unknown Source)
04-15 12:37:15.791: W/System.err(6898): at net.sourceforge.pinyin4j.ChineseToPinyinResource.<init>(Unknown Source)
04-15 12:37:15.791: W/System.err(6898): at net.sourceforge.pinyin4j.ChineseToPinyinResource.<init>(Unknown Source)
04-15 12:37:15.791: W/System.err(6898): at net.sourceforge.pinyin4j.ChineseToPinyinResource$ChineseToPinyinResourceHolder.<clinit>(Unknown Source)
04-15 12:37:15.791: W/System.err(6898): at net.sourceforge.pinyin4j.ChineseToPinyinResource.getInstance(Unknown Source)
04-15 12:37:15.796: W/System.err(6898): at net.sourceforge.pinyin4j.PinyinHelper.getUnformattedHanyuPinyinStringArray(Unknown Source)
04-15 12:37:15.796: W/System.err(6898): at net.sourceforge.pinyin4j.PinyinHelper.getFormattedHanyuPinyinStringArray(Unknown Source)
04-15 12:37:15.796: W/System.err(6898): at net.sourceforge.pinyin4j.PinyinHelper.toHanyuPinyinStringArray(Unknown Source)
04-15 12:37:15.796: W/System.err(6898): at com.sf.tools.HanziHelper.char2Pinyin(HanziHelper.java:29)
04-15 12:37:15.796: W/System.err(6898): at com.sf.tools.HanziHelper.words2Pinyin(HanziHelper.java:44)
04-15 12:37:15.796: W/System.err(6898): at com.sf.parse.PeopleListParser$Result$People.compareTo(PeopleListParser.java:156)
04-15 12:37:15.796: W/System.err(6898): at com.sf.parse.PeopleListParser$Result$People.compareTo(PeopleListParser.java:1)
04-15 12:37:15.796: W/System.err(6898): at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:286)
04-15 12:37:15.796: W/System.err(6898): at java.util.ComparableTimSort.sort(ComparableTimSort.java:153)
04-15 12:37:15.796: W/System.err(6898): at java.util.ComparableTimSort.sort(ComparableTimSort.java:142)
04-15 12:37:15.796: W/System.err(6898): at java.util.Arrays.sort(Arrays.java:1974)
04-15 12:37:15.796: W/System.err(6898): at java.util.Collections.sort(Collections.java:1941)
04-15 12:37:15.796: W/System.err(6898): at com.sf.activity.PeopleListActivity.initPageView(PeopleListActivity.java:73)
04-15 12:37:15.796: W/System.err(6898): at com.sf.activity.ReceiverListActivity.initPageView(ReceiverListActivity.java:23)
04-15 12:37:15.796: W/System.err(6898): at com.yek.android.base.BaseActivity.onCreate(BaseActivity.java:158)
Then I changed to use Collator usCollator = Collator.getInstance(Locale.SIMPLIFIED_CHINESE); usCollator.setStrength(Collator.PRIMARY);
It works well if only there are Chinese characters. In the example above, this method will sort Chinese and English separately.
So, do you have any ideas?