IOS arrays are sorted alphabetically by Chinese keywords

Source: Internet
Author: User
Tags sorted by name

In IOS projects, contacts or members in the address book are sorted by name as keywords, because nsarray does not directly support sorting of Chinese characters, which requires converting Chinese characters into pinyin to complete ~ Z sorting seems to be a headache, because it involves converting Chinese characters into pinyin, kmyhy provides a easier way to get the first letter of a Chinese character, for example, change "Wang" to "W" and complete the article (portal ).

He uses the pinyinfirstletter function to obtain Chinese pinyin. The function principle is: "pinyinfirstletter is based on such a simple principle: we know that in Objective C, strings are encoded in Unicode. In the Unicode Character Set, the encoding range of Chinese characters is between 4e00 and 9fa5 (that is, the first 19,968th characters starting from 20902 are simplified Chinese characters ). We store the initials of these characters in a char array in sequence. When we look for the first letter of a Chinese character, we only need to convert the Unicode code of this Chinese character (that is, the mandatory conversion of Char to int) minus 19968, then use this number as the index to find the characters in the char array." FunctionCodeI also posted it.

 
Char pinyinfirstletter (unsignedshort Hanzi) {int Index = Hanzi-hanzi_start; If (index> = 0 & index <= hanzi_count) {return firstletterarray [Index];} else {return Hanzi ;}}


This method is a very practical solution, but there is also a problem: it does not support sorting of the second letters of Chinese characters (first words, it is said that the char array mapped will be large. Here I will give my method. I will first look at what can be used in the cocoatouch framework. In nsstring, there is a function localizedcompare:. Its function is to compare itself with a given string, returns a localized comparison result, that is, This function supports Chinese character comparison.
Further localizedcompare: it is only a comparison between strings and strings. We need to sort the nsarray array by keyword for Chinese character strings. We continue to work hard on the nsarray SDK and find that apart from sortdarrayusingdescriptors: sort by nssortdescriptor and sotedarrayusingfunction: Context:. This function allows us to customize a function to set comparison rules and return comparison results. This solves the problem. I will post the code first.

 
Nsinteger nicknamesort (ID user1, Id user2, void * context) {user * u1, * U2; // type conversion U1 = (User *) user1; U2 = (User *) user2; return [u1.nickname localizedcompare: u2.nickname];}


Call the comparison function where comparison is needed.

 
Sortarr = [arr sortedarrayusingfunction: nicknamesort context: NULL];

few codes are required, which has the following benefits: 1 supports sorting Multiple Chinese Characters in alphabetical order (if the first letter of the first word is the same, it is compared by the second letter of the first word. If the first letter is the same, by the first letter of the second word ). 2. It may have to be saved in Chinese pinyin. It is not needed now. 3. You can further customize nicknamesortde to perform more complex comparisons. For example, you can first compare the member status and complete the comparison in alphabetical order of names. 4. The overall structure is simple to use in the cocatouch framework.
again, cocatouch is amazing !!

Related Article

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.