Nsarary Custom Object Sort Nscomparator, compare

Source: Internet
Author: User

Reference from:http://mobile.51cto.com/hot-434804.htm

1. Building the Person class

Person.h

@interface Person:nsobject@property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *surname; @pro Perty (nonatomic, strong) NSDate *dateofbirth; @end

2. Initialize the Objsort object sorting function, including a nsarray array, the array element is a custom person

ObjSort.h

/** * Nsarray Custom Object Sort class */@interface objsort:nsobject-(void) Personssort_nscomparator;//nscomparator comparator @end

Objsort.m

@interface Objsort () @property (nonatomic, strong) Nsmutablearray *personobjs;// An array that stores several person class objects @end@implementation objsort/** * Initialize the object * Initializes the initial contents of the Nsmutablearray array personobjs, wraps the Person class object */-(    Instancetype) init{self = [super init];    if (self = = nil) {return nil;    } nsdateformatter *formatter = [[NSDateFormatter alloc] init];        [Formatter setdateformat:@ "dd/mm/yyyy"];    Person *person[5];    for (int i=0, i<5; i++) {person[i] = [[Person alloc] init];    } person[0].name = @ "Smith";    Person[0].surname = @ "John";        Person[0].dateofbirth = [Formatter datefromstring:@ "03/01/1984"];    Person[1].name = @ "Andersen";    Person[1].surname = @ "Jane";        Person[1].dateofbirth = [Formatter datefromstring:@ "16/03/1979"];    Person[2].name = @ "Clark";    Person[2].surname = @ "Anne";        Person[2].dateofbirth = [Formatter datefromstring:@ "13/09/1995"];    Person[3].name = @ "Smith";    Person[3].surname = @ "David"; Person[3].dateofbirth = [ForMatter datefromstring:@ "19/07/1981";    Person[4].name = @ "Johnson";    Person[4].surname = @ "Rose";    Person[4].dateofbirth = [Formatter datefromstring:@ "22/02/1989"];    if (_personobjs==nil) {_personobjs = [[Nsmutablearray alloc] initwithcapacity:5];    } for (int i=0; i<5; i++) {[_personobjs addobject:person[i]];    } [self SHOWPERSONS:_PERSONOBJS]; return self;}        /** * Log Print output current Nsarray content */-(void) Showpersons: (Nsarray *) array{person *person;    if (_personobjs = = nil) {NSLog (@ "nil");        } for (int i =0; i< [array Count], i++) {person = [array objectatindex:i];            if ([Person Iskindofclass:[person class]]) {NSLog (@ "---person%d---", i);            NSLog (@ "Name:%@", person.name);            NSLog (@ "Surname:%@", person.surname);        NSLog (@ "dateOfBirth:%@", Person.dateofbirth); } else{NSLog (@ "object in Personobjs was not the person class Object.");}}
NSLog (@ "All person print over.\n\n");}

Method One: Nscomparator block + Nsarray's Sortedarrayusingselector

Implement the method in Objsort Personssort_nscomparator

/** * Sorting method using nscomparator comparison */-(void) personssort_nscomparator{        nscomparator sortblock = ^ (person *p1, person *p2) { C1/>nscomparisonresult r1 = [P1.name compare:p2.name];        if (r1! = nsorderedsame) {            return r1;        } else {            Nscomparisonresult r2 = [P1.surname compare:p2.surname];            if (r2! = nsorderedsame)                return R2;            else                return [P1.dateofbirth Compare:p2.dateOfBirth];        }    ;    Nsarray *personlist = [Self.personobjs sortedarrayusingcomparator:sortblock];    Nsarray *personlist = [Self.personobjs sortedarrayusingselector: @selector (compare:)];    [Self showpersons:personlist];}

Method Two: Using the object's custom method compare:

Add in PERSON.M

-(Nscomparisonresult) Compare: (Person *) otherperson{    nscomparisonresult r1 = [Self.name compare:otherPerson.name ];    if (r1! = nsorderedsame) {        return r1;    } else {        Nscomparisonresult r2 = [Self.surname compare:otherPerson.surname];        if (r2! = nsorderedsame)            return R2;        else            return [Self.dateofbirth Compare:otherPerson.dateOfBirth];    }    return [Self.name compare:otherPerson.name];}

Add in OBJSORT.M

/** * Use custom object Custom Function compare for Nsarray sort */-(void) personssort_customcompare{nsarray *personlist    = [Self.personobjs Sortedarrayusingselector: @selector (compare:)];    [Self showpersons:personlist];}

Add a response interface in ObjSort.h

/** * Nsarray Custom Object Sort class */@interface objsort:nsobject-(void) Personssort_nscomparator;//nscomparator comparator-(void) personssort_customcompare;//Object Custom Comparison method Compare@end

Finally, at main.m (sort call), add the

        Objsort *objsort = [[Objsort alloc] init];        [Objsort Personssort_nscomparator];

Method One result

---person0---Name:SmithSurname:JohnDateOfBirth:1984- on- Geneva  -:xx:xx+0000---person1---Name:AndersenSurname:JaneDateOfBirth:1979-Geneva- the  -:xx:xx+0000---person2---Name:ClarkSurname:AnneDateOfBirth:1995- the- A  -:xx:xx+0000---person3---Name:SmithSurname:DavidDateOfBirth:1981- -- -  -:xx:xx+0000---person4---Name:JohnsonSurname:RoseDateOfBirth:1989- Geneva- +  -:xx:xx+0000All is print over.---person0---Name:AndersenSurname:JaneDateOfBirth:1979-Geneva- the  -:xx:xx+0000---person1---Name:ClarkSurname:AnneDateOfBirth:1995- the- A  -:xx:xx+0000---person2---Name:JohnsonSurname:RoseDateOfBirth:1989- Geneva- +  -:xx:xx+0000---person3---Name:SmithSurname:DavidDateOfBirth:1981- -- -  -:xx:xx+0000---person4---Name:SmithSurname:JohnDateOfBirth:1984- on- Geneva  -:xx:xx+0000All is print over.

Method Two Results

---person0---Name:SmithSurname:JohnDateOfBirth:1984- on- Geneva  -:xx:xx+0000---person1---Name:AndersenSurname:JaneDateOfBirth:1979-Geneva- the  -:xx:xx+0000---person2---Name:ClarkSurname:AnneDateOfBirth:1995- the- A  -:xx:xx+0000---person3---Name:SmithSurname:DavidDateOfBirth:1981- -- -  -:xx:xx+0000---person4---Name:JohnsonSurname:RoseDateOfBirth:1989- Geneva- +  -:xx:xx+0000All is print over.---person0---Name:AndersenSurname:JaneDateOfBirth:1979-Geneva- the  -:xx:xx+0000---person1---Name:ClarkSurname:AnneDateOfBirth:1995- the- A  -:xx:xx+0000---person2---Name:JohnsonSurname:RoseDateOfBirth:1989- Geneva- +  -:xx:xx+0000---person3---Name:SmithSurname:DavidDateOfBirth:1981- -- -  -:xx:xx+0000---person4---Name:SmithSurname:JohnDateOfBirth:1984- on- Geneva  -:xx:xx+0000All is print over.

Nsarary Custom Object Sort Nscomparator, compare

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.