Go Java kanji sorted by pinyin

Source: Internet
Author: User

Recent projects on the use of Chinese character sorting problem, online Search after a general use of the following method comparison.

@Test    Public void Test_sort_pinyin () {      = collator.getinstance (java.util.Locale.CHINA);       = {"Zhang San", "John Doe", "Harry", "Zhao Liu", "JAVA", "123", "$%$#", "haha a",              "1 haha a", "1 haha B", "1 haha a", "haha", "Ha", "Yi" };
    list<String> List = arrays.aslist (arr);      Arrays.sort (arr, CMP);      SYSTEM.OUT.PRINTLN (list);  }  

The output results are as follows:

[$%$#, 123, 1 haha A, 1 haha A, 1 haha b, JAVA, ha, haha, haha A, John Doe, Harry, Zhang San, Zhao Liu, Yee]

Found that there is a bug, because the internet also saw some friends ask questions, so the test deliberately add pleasant words. Found the "Yi" word is finally. So we can only find a solution. Later, I remembered the class that I used to use when I turned Chinese automatically into pinyin.

PINYIN4J Open Source project can be viewed specifically http://pinyin4j.sourceforge.net/using: Pinyin4j-2.5.0.jar this package;

I really have to thank this person.

After encapsulation into the comparator code is as follows:

ImportJava.util.Comparator; ImportNet.sourceforge.pinyin4j.PinyinHelper; /*** Chinese characters in alphabetical order by comparison device *@authorKennylee 2009-2-23 10:08:59 **/   Public classPinyincomparatorImplementsComparator<object> {       Public intCompare (Object O1, Object O2) {CharC1 = (String) O1. CharAt (0); CharC2 = (String) O2. CharAt (0); returnConcatpinyinstringarray (Pinyinhelper.tohanyupinyinstringarray (C1)). CompareTo (Con      Catpinyinstringarray (pinyinhelper. Tohanyupinyinstringarray (C2))); }      PrivateString Concatpinyinstringarray (string[] pinyinarray) {StringBuffer PINYINSBF=NewStringBuffer (); if((Pinyinarray! =NULL) && (pinyinarray.length > 0)) {               for(inti = 0; i < pinyinarray.length; i++) {pinyinsbf.append (pinyinarray[i]); }          }          returnpinyinsbf.tostring (); }  }  

Continue the test

@Test    Public void  test_sort_pinyin2 () {      = {"Zhang San", "John Doe", "Harry", "Zhao Liu", "JAVA", "123", "$%$#", "haha a",              "1 haha a", "1 haha b", "1 haha a", "haha", "Ha", "Yi Sentiment" };      List<String> list = arrays.aslist (arr);       New pinyincomparator ());      SYSTEM.OUT.PRINTLN (list);  }  

The final print effect is as follows:

[JAVA, 123, $%$#, 1 haha A, 1 haha b, 1 haha A, haha a, haha, ha, John Doe, Harry, Yi sentiment, Zhang San, Zhao Liu]

But it also shows that there are some controversies about other sorts, such as symbols, but the relationship is small. Or you can also judge whether the first word is Chinese, if it is not followed by other appropriate comparison method. The benefit of the comparator is that it can be customized.

In the example, the array is used only to sort, but perhaps more often than not, the sort of list is encountered, that is, list<object>. The same can be extrapolate to modify the Compare method in the comparator, as long as the extraction of the Chinese characters used to sort the key fields out.

Then the comparison is called when the

Collections.sort (List,new pinyincomparator ());

And note that the example only intercepts the first character for sorting, if necessary can be further sorted.

But the main or Chinese sort is really correct.

Basic principle:

In fact, to get it, the principle is to turn the characters into pinyin, and then to sort it. So if you have encountered the need to convert Chinese characters into pinyin in the project. If you encounter Chinese characters, the problem will be solved naturally.

Also have to sigh, in China to do research and development than foreign countries to be hard ah. At least the character set is so much, and it doesn't get a lot of basic support, and then it raises a series of questions. such as garbled, Chinese character sorting, pinyin and so on ...

But to solve a write down one, you can use it directly later, to learn a little more.

Transferred from: http://blog.csdn.net/kennylee26/article/details/3926168

Go Java kanji sorted by pinyin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.