我有一个String列表,每个列表可能由中文字符或数字或英文字符组成。 (例如:“ z莉z莉”,“露西”,“蒋豪”,“ qwer”,“ zout1iao”,“ hah”)。我想要做的是按照英文字母顺序对它们进行排序,例如Android的“通讯录”应用。(即“ hah”,“蒋豪”,“露西”,“ qwer”,“ z莉z莉”,“ zout1iao”)策略是将所有汉字都转换成拼音,然后照常进行比较。
我尝试了yinpin4j库,它在J2SE平台上运行良好,但是当我在Android上运行相同的代码时,它引发了错误。
Java代码是:
@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();
}
错误是:
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)
然后我改为使用
Collator usCollator = Collator.getInstance(Locale.SIMPLIFIED_CHINESE); usCollator.setStrength(Collator.PRIMARY);
如果只有中文字符,则效果很好。在上面的示例中,此方法将分别对中文和英文进行排序。
那么,您有什么想法吗?
最佳答案
通讯录应用使用“ COLLATE LOCALIZED ASC”。你可以尝试同样的事情
What does COLLATE LOCALIZED ASC stand for?