Array Inner object sort (sort)

Source: Internet
Author: User

1. Array sorting There are many ways such as For,while loop to do bubble sort or quick look, sort and so on various sorting methods

And what I'm going to say here is how the Apple API provides several system methods

A. Iterator descriptor

B. Method comparison Selector

C. functions comparison function

D. Blocks code block customization

2. Example

1.1 A class to compare objects with (person)

@interface Person:nsobject@property (nonatomic, copy) NSString * name; @property (nonatomic, copy) NSString * phone; @prope Rty (nonatomic, assign) int age; @end

1.2 Customizing several person objects (in other classes)

    Person *person1 = [[Person alloc] init];    Person1.name = @ "Gulong";    Person1.age =;        Person *person2 = [[Person alloc] init];    Person2.name = @ "Guzheng";    Person2.age =;        Person *person3 = [[Person alloc] init];    Person3.name = @ "Zhangsan";    Person3.age = +;        Person *person4 = [[Person alloc] init];    Person4.name = @ "Lisi";    Person4.age = +;        Nsarray *originalarray = @[person1,person2,person3,person4];

1.3 Sort Start

1.3.1 Using iterators for sorting

Multi-criteria sequencing is easy to use with Nssortdescriptor

    Nssortdescriptor *sorter1 = [[Nssortdescriptor alloc] initwithkey:@ "name" Ascending:yes];    Nssortdescriptor *sorter2 = [[Nssortdescriptor alloc] initwithkey:@ "age" ascending:no];    Nsarray *modifyarray1 = [Originalarray sortedarrayusingdescriptors:[nsarray arraywithobjects:sorter1,sorter2,nil];

Sort by the letter of name first, then the age-size comparison to sort in descending order

Arrays can be multi-sorted by Sortedarrayusingdescriptors (the next comparison iterator when equal)

1.3.2 Sorting using SEL methods

you first need to define it inside the object class and implement it within the implementation class (OC method), which returns a The Nscomparisonresult enumeration value (which is actually -1,0,1), and then Nsarray calls the Sortedarrayusingselector method when the corresponding comparison sort is made internally

Person.h
-(Nscomparisonresult) Compareageascende: (ID) Other;
Person.m
-(Nscomparisonresult) Compareageascende: (ID) other{person *otherperson = (person *) and other ; if (Self.age > Otherperson.age) { return nsordereddescending; } else{ if (self.age = = otherperson.age) { return nsorderedsame; } else{ return nsorderedascending;}}}

Here is the call to Sortedarrayusingselector

Viewcontroller  (-(void) viewdidload)  Nsarray *modifyarray2 = [Originalarray sortedarrayusingselector:@ Selector (Compareageascende:)];  NSLog (@ "ModifyArray2:%@", modifyArray2);

Note: A. You can use compare when the object in the array is nsstring: sel comparison

1.3.3 function Comparison This function is a comparison function (C language), the function is also put back Nscomparisonresult This enumeration, is actually a nsinteger integer, and then Nsarray call sortedarrayusingfunction This method, the corresponding comparison sort is performed internally.

Defined in the Viewcontroller (in fact, where the definition is irrelevant, mainly can be called to the line) Nsinteger Personsort (id obj1,id obj2, void * context) {person    * Person1 = (person *) obj1;    Person *person2 = (person *) obj2;    return [Person1.name localizedCompare:person2.name];}

Nsarray *modifyarray3 = [Originalarray sortedarrayusingfunction:personsort context:null];    NSLog (@ "ModifyArray3:%@", modifyArray3);

1.3.4 Block comparison The block defines a code block that writes a comparison, and the code block also returns the Nscomparisonresult enumeration

Nsarray *modifyarray4 = [Originalarray sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {person        * Pers1 = (person *) obj1;        Person *pers2 = (person *) obj2;        if (Pers1.age > Pers2.age) {            return nsordereddescending;        } else if (Pers1.age < pers2.age) {            return nsorderedascending;        } else{            return nsorderedsame;        }    ];    NSLog (@ "ModifyArray4:%@", ModifyArray4);

1.4 String (with Chinese characters) to pinyin string (for more requirements above for comparison issues)

-(NSString *) Phonetic: (nsstring*) sourcestring {    nsmutablestring *source = [sourcestring mutablecopy];    Cfstringtransform ((__bridge cfmutablestringref) source, NULL, Kcfstringtransformmandarinlatin, NO);    Cfstringtransform ((__bridge cfmutablestringref) source, NULL, Kcfstringtransformstripdiacritics, NO);    return source;}
Chinese characters will be separated by a space after pinyin

Sort the array's inner object (sort)

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.