3

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?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Longerian
  • 723
  • 5
  • 13
  • do you want to sort Chinese and English characters using the same method? – UVM Apr 15 '12 at 05:01
  • yes, convert Chinese character to pinyin, than they could be sort with English together. – Longerian Apr 15 '12 at 05:41
  • 2
    when you do sort operartion in java, it uses unicode only.That said,depending on the locale of the user, automatically this is adjusted.ex, if chinese user can see sorted text if his locale of the phone set to chinese.Also, if the locale is set to english, then by default it will displayed as sorted. – UVM Apr 15 '12 at 06:08

4 Answers4

0

Contacts app uses "COLLATE LOCALIZED ASC". You can try the same thing

What does COLLATE LOCALIZED ASC stand for?

Community
  • 1
  • 1
Ankur Kumar
  • 972
  • 7
  • 12
0

My advice would be, parse the "word" codepoint by codepoint and build a new string. If the next codepoint's Latin/Common, don't call words2Pinyin(), and just add the chat to the newly built string. It it's Chinese, call words2Pinyin() and add the pinyin to the new string. Loop. At the end you have a clean, latin string.

dda
  • 6,030
  • 2
  • 25
  • 34
  • Thank you for your advice. The fact is I set up a new specific project to run that demo, and it works. But I still don't know how that error occurs. – Longerian May 22 '12 at 01:55
0

you can google a jar seems pinyin.jar,it can help you

pengwang
  • 19,536
  • 34
  • 119
  • 168
0

If you are pulling contacts data from phone (e.g. name, phone number, email, ...), try to pull one more column ContactsContract.Contacts.SORT_KEY_PRIMARY, it stores the display name's pinyin together with display name(if any).

Let's say DISPLAY_NAME "阿妹", its SORT_KEY_PRIMARY will be "a 阿 MEI 妹"; "大姨", the sortkey is "DA 大 YI 姨". From there you can easily apply alphabet sorting and map to the original display name. :)

Beeing Jk
  • 3,796
  • 1
  • 18
  • 29