Chinese first alphabetical order

Source: Internet
Author: User

Because the item needs to sort contacts in the Address Book, you need to convert the Chinese characters to pinyin. In fact, this conversion is not as difficult as it is imagined (because we just turn the Chinese into pinyin initials, such as "Wang" converted to the letter W can be, and do not need to convert to the full pinyin Wang). In this case, we found a simple solution: a foreigner (the author named George in the code) wrote a Pinyinfirstletter function in C to get the Chinese pinyin initials.

This function is based mainly on a huge C-language char array that maps all the Chinese pinyin initials in the Unicode character set.

Pinyinfirstletter is based on a simple principle: we know that in the Objective C language, strings are encoded in Unicode. In the Unicode character set, the encoding range for Chinese characters is between 4E00 and 9fa5 (that is, the 20,902 characters starting from the 19,968th are Simplified Chinese character). We place the pinyin initials of these characters in a char array in the order in which they are placed. When we look for the pinyin initials of a Chinese character, we simply subtract 19968 from the Unicode code of the kanji (that is, the char is cast to int) and use this number as an index to find the letters stored in the char array.

So, with this char array as the base, everything becomes very simple:

Char pinyinfirstletter (unsignedshort Hanzi)

{

int index = Hanzi-hanzi_start;

if (index >= 0&& index <= hanzi_count)

{

Returnfirstletterarray[index];

}

Else

{

return Hanzi;

}

}

As for the sort, we can put the sort of Chinese (for simplicity, we assume that these sorts of Chinese have only one word) put in the Nsarray, and then use the Nsarray sortedarrayusingcomparator: method to sort. Sortedarrayusingcomparator: The method is a comparison of the inner elements of the nsarray. You just need to provide a block for this method as an argument. Then in this block, compare the size of the two elements and return a Nscomparisonresult (the Nscomparisonresult enumeration is used to represent the result of the comparison: equals, less, greater than):

Nsarray *sortedarray=[marray sortedarrayusingcomparator:^ (id A, id b) {

Char C1=pinyinfirstletter ([(nsstring*) a characteratindex:0]);

Char C2=pinyinfirstletter ([(nsstring*) b characteratindex:0]);

nsstring* s1=[[nsstringstringwithformat:@ "%c", C1] uppercasestring];

nsstring* s2=[[nsstringstringwithformat:@ "%c", C2] uppercasestring];

return [S1 COMPARE:S2];

}];

The following is the effect of the program running. You in the "Chinese" column, enter a few Chinese, and then click the "Convert" button, in the "Sort Results" column below to show the number of Chinese first alphabetical order results.

Chinese first alphabetical order

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.