iOS array sorted alphabetically by Chinese keyword

Source: Internet
Author: User

This article reprinted to http://blog.csdn.net/xunyn/article/details/7882087iosobjective Cuser Frame Communication

The iOS project will use the contact person or the member by the name of the keyword sort, because Nsarray does not directly support the sorting of Chinese characters, this will be done by converting Chinese characters to Pinyin to complete the sort by a~z, which seems to be a headache problem, because the Chinese characters into pinyin, Kmyhy gives an easier way to obtain the first letter of the first word of the Chinese character, such as "King" into "W", complete article (portal).

Where he obtains Chinese pinyin through the Pinyinfirstletter function: "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. "I also posted the function code.

[CPP]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. Char pinyinfirstletter (unsignedshort Hanzi)
    2. {
    3. int index = Hanzi-hanzi_start;
    4. if (index >= 0&& index <= hanzi_count)
    5. {
    6. return Firstletterarray[index];
    7. }
    8. Else
    9. {
    10. return Hanzi;
    11. }
    12. }

This method is really a very basic thing to solve a clever problem, but there is a problem: not support the Chinese character (first word) of the second letter of the sort, it is said that the map of the char array will be large. Here I give my method, first in the Cocoatouch frame to see what can be used, in NSString has a function localizedcompare:, its function is through itself and the given string comparison, return a localized comparison results, This means that the function is to support the comparison of Chinese characters.
Further localizedcompare: just string and string comparison, we want to let the Nsarray array by keyword for the Chinese character string ordering, we continue to work on the Nsarray SDK, We found that except sortdarrayusingdescriptors: Sort by Nssortdescriptor, and Sotedarrayusingfunction:context: This function allows us to customize a function to make comparison rules , and return the results of the comparison, so the problem will be solved. I'll post the code first.

[CPP]View Plaincopyprint? < param name= "wmode" value= "Transparent" >
    1. Nsinteger Nicknamesort (ID user1, id user2, void *context)
    2. {
    3. User *U1,*U2;
    4. Type conversions
    5. u1 = (user*) user1;
    6. U2 = (user*) user2;
    7. return [U1.nickname localizedCompare:u2.nickName
    8. ];

Call the comparison function where you need to compare
[CPP]View Plaincopyprint?
    1. Sortarr = [arr sortedarrayusingfunction:nicknamesort context:null];

There are few code requirements, and there are several advantages to this: 1 support for multiple Chinese characters in alphabetical order (if the first letter of the first word is the same, then the second letter of the first word is compared, if the first letter is exactly the same, the letter of the second word continues to sort). 2 may need to save the Chinese pinyin place, now do not need. 3 can be further customized by the NICKNAMESORTDE, to complete a more complex comparison, such as the first comparison of membership status, in alphabetical order by name to complete the comparison. 4 The overall structure is simple to use the method under the Cocatouch framework.
Another sigh, cocatouch you really powerful!!

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.