"IOS" sets a group of data containing Chinese according to #abc ... Z? grouping

Source: Internet
Author: User

previous article "IOS" mimicsWindowsPhone List Index Control Yfmetrolistboxinside

We step-by-step implementation of the WindowsPhone style index.

However, if you want to implement alphabetical order, you have to fill in the data yourself, and not be able to automatically categorize it.

So this article is about how to sort an array.

The # in the title represents a number, and represents a symbol other than a number, Chinese and English.

1. Requirements: classify strings into numbers, Chinese and English initials, and other symbols in three categories.

Numbers, symbols in alphabetical order, in each letter category, English is always in front of Chinese

This mainly involves the sorting of Chinese and English.

2. Detours:

  1. There is a pinyin.h on the network. Be able to get the first letter of the Chinese, but only get the first letter of each word for us

It's still not much use. Two Chinese with the same initials do not know which one is in front and which is behind.

2. Just a Chinese language, it is not possible to determine which of the Polyphone (of course, this essay also does not involve the processing of polyphone)

3. Reserve:

So the best way to do this is to convert Chinese to phonetic strings, and then sort the strings.

To convert Chinese to Pinyin, iOS has given the method (the efficiency of what the temporary regardless)

-(NSString *) transformtopinyin{    *mutablestring = [nsmutablestring stringwithstring:self]; //self refers to the character string     false );    //Pinyin with tones  false ); //Remove tones      return mutablestring;}

Use regular expressions to determine whether the first digit of a string is numeric, Chinese, or English

-*firsetstr = [self substringtoindex:1@ "[\u4e00-\u9fa5]";   Chinese        @ "[a-za-z\\s]" is in English          @ "[0-9]" for digital nspredicate *predicate = [Nspredicate predicatewithformat  :@ "selfMATCHES%@", regex];  return  [predicate evaluatewithobject:firsetstr];}

Each of the classes we classify is an array

You can use the custom function sortedarrayusingfunction to sort.

[Groupedarray Addobject:[numberarray Sortedarrayusingfunction:normalsort context:null];

You can compare two strings by Localizedcompare this method

Nsinteger Normalsort (IDIDvoid *context) {    *str1 = (NSString *) string1;    *STR2 = (NSString *) string2;     = [str1 localizedcompare: (nsstring*) str2]; return result;}

4. Resolution:

Now let's implement the data that we need to use to assemble them into the yfmetrolistbox that we inherit from UITableView. The following are obtained through static methods (class methods).

1.IndexArray; //headerview Header Array

-(NSString *) getfirstletter{if([self startwithnumber]) {returnKnumbersign;//#数字}Else if([self Startwithchinese] | |[self startwithenglish]) {NSString*string=[self Changetoenglishorchinese]; NSString*firstletter = [stringHasprefix:kdistinguishstring]? [stringSubstringwithrange:nsmakerange (Kdisti nguishstring.length,1)] : [stringSubstringtoindex:1]; //Get the first letter       returnFirstletter; }Else{returnKsymbolsign;//? Other}}-(NSString *) changetoenglishorchinese{NSString*alphabet; if([self Startwithchinese]) {//1. Chinesealphabet=[self transformtopinyin]; }Else{//2. Increase in English @ "0" kdistinguishstring guarantee the comparison is less than the Chinese first letterAlphabet =[kdistinguishstring stringbyappendingstring:self];} returnAlphabet }

How to ensure that after the conversion pinyin Pinyin and English is always in front of the Chinese language?

I was thinking of adding a character less than @ "a" before Pinyin: @ "0";

After getting the first-letter array of headings you can and the whole (#ABC ... Z, all) to compare, and see what's available.

2.groupedArray; //Classification category (#. A.B.C ... Z.?) Array of Components

First, the incoming array is divided into 3 categories, digital, Chinese and English (due to the special processing in English, so it can be compared together), the other three categories.

Each class is then sorted from small to large.

 //To order in Chinese and English
Nsarray *indexarray = [self getindexarray:alphabetarray]; // get Indexarray Header array Nsarray *headerarray = [self getfirstletterarray:alphabetarray]; //Get an array of initials in Chinese and English
For (int i = 0; i < Indexarray.count; i++) {
Nsmutablearray *alphaarray = [Nsmutablearray array];
For (int j = 0; j < Alphabetarray.count; J + +) {   
nsstring *headstring = headerarray[j];
if([[[Headstring uppercasestring] isequaltostring:indexarray[i]) {
[Alphaarray Addobject:alphabetarray[j]];
}
}
[Groupedarray Addobject:[alphaarray sortedarrayusingfunction:cnandensort context:null]; //Sort each of the arrays
}

3.GroupedDictionaryArray: //Through the above two, two sets of data have been obtained, can now be assembled in the form of a dictionary (including two types)

1. Similar to @[{@ "Indexkey": @ "A", @ "Arraykey": @[@ "abandon", @ "aha", @ "all"]}], two key-value pairs for each title and corresponding content array

+ (Nsarray *) Getgroupeddictionaryarray: (Nsarray *) array;

2. Similar @[{@ "a": @[@ "abandon", @ "aha", @ "all"]}], a key-value pair, key holds the title, value holds the corresponding array of contents

+ (Nsarray *) Getgroupeddictionaryarray: (Nsarray *) array Indexkey: (NSString *) Indexkey Arraykey: (NSString *) ArrayKey;

4. Since many of this class are static methods, non-static methods cannot be used internally, and many are processing nsstring strings,

Because I wrote a classification in this class for the first letter type of the string to judge, intercept, transform, and so on.

5. Results:

Nsarray *array = [Yfgroupeddata getgroupeddictionaryarray:[@"Once",@"Begin Again",@"Hello wolrd",@"~",@"Windows",@"Lumia",@"Sophie",@"Yvan Wang",@"Super-energy Marines",@"Angry Birds",@")",@"%",@"Windows Phone",@"950XL",@"Speed and passion",@"1520",@"Titanic",@"Farewell My Concubine",@"Captain America",@"World of Warcraft",@"Interstellar crossing",@"Rebel Lulu repair.",@"The Lost lover",@"The love of Siam",@"Hormones",@"Love and Home",@"a gift from room 7th ,",@"Spring and Winter",@"The Little Prince",@"descendants of the Sun",@"Non-summit talks",@"the little thing about first love",@"",@"Baby and Me",@"Crazy Animal City",@" old boy",@"my girl.",@"run, brother.",@"I'm a god."]indexkey:kindexkey Arraykey:karraykey];

Let's take a look at some of the empty ones already divided into parts.

Get it done!

Github:yfgroupeddata

"IOS" sets a group of data containing Chinese according to #abc ... Z? grouping

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.