Main.m
1 #import<Foundation/Foundation.h>2 #import "Person.h"3 /**4 Nssortdescriptor can be sorted by the properties of the object, and multiple attribute ordering is supported. For example, we have a person object that has a name and age two attributes, and we need to output person object array information according to the man's aging property (descending) and the Name property (ascending)5 */6 voidTestsortdescriptor () {7Nsmutablearray *marrperson = [[Nsmutablearray alloc] Initwithcapacity:5];8Person *p = [[Person alloc] Initwithname:@"KK"Age at];9 [Marrperson addobject:p];Tenp = [[Person alloc] Initwithname:@"Candy"Age A]; One [Marrperson addobject:p]; Ap = [[Person alloc] Initwithname:@"Wiky"Age -]; - [Marrperson addobject:p]; -p = [[Person alloc] Initwithname:@"Stone"Age +]; the [Marrperson addobject:p]; -p = [[Person alloc] Initwithname:@"Tom"Age -]; - [Marrperson addobject:p]; -p = [[Person alloc] Initwithname:@"Sherlock"Age -]; + [Marrperson addobject:p]; -p = [[Person alloc] Initwithname:@"Alex"Age in]; + [Marrperson addobject:p]; Ap = [[Person alloc] Initwithname:@"Keye"Age -]; at [Marrperson addobject:p]; - -NSLog (@"Age property by person (descending) and Name property (ascending)"); -Nssortdescriptor *sortbyage = [[Nssortdescriptor alloc] Initwithkey:@" Age"Ascending:no]; -Nssortdescriptor *sortbyname = [[Nssortdescriptor alloc] Initwithkey:@"name"Ascending:yes]; - [Marrperson Sortusingdescriptors:@[sortbyage, Sortbyname]]; in for(Person *pinchMarrperson) { -NSLog (@"age=%ld, name=%@", P.age, p.name); to } + } - intMainintargcConst Char*argv[]) { the @autoreleasepool { * testsortdescriptor (); $ }Panax Notoginseng return 0; -}
Person.h
1 #import <Foundation/Foundation.h>2@interface person:nsobject3 @property (nonatomic, Strong) NSString *name; 4 @property (nonatomic, assign) Nsinteger age; 5 6 -(ID) Initwithname: (NSString *) name Age: (Nsinteger) age; 7 @end
Person.m
1 #import "Person.h"2 3 @implementation Person4- (ID) Initwithname: (NSString *) name Age: (Nsinteger) Age {5 if(self =[Super Init]) {6_name =name;7_age =Age ;8 }9 returnSelf ;Ten } One @end
Results:
1 --Geneva- - A: -:13.240ocnssortdescriptor[1276:49262] by person's age property (descending) and the Name property (ascending)2 --Geneva- - A: -:13.241ocnssortdescriptor[1276:49262] Age= +, name=Stone3 --Geneva- - A: -:13.241ocnssortdescriptor[1276:49262] Age= in, name=Alex4 --Geneva- - A: -:13.241ocnssortdescriptor[1276:49262] Age= -, name=Keye5 --Geneva- - A: -:13.241ocnssortdescriptor[1276:49262] Age= -, name=Tom6 --Geneva- - A: -:13.242ocnssortdescriptor[1276:49262] Age= -, name=Sherlock7 --Geneva- - A: -:13.242ocnssortdescriptor[1276:49262] Age= -, name=Wiky8 --Geneva- - A: -:13.242ocnssortdescriptor[1276:49262] Age= at, name=KK9 --Geneva- - A: -:13.242ocnssortdescriptor[1276:49262] Age= A, Name=candy
The Nssortdescriptor of Objective-c grammar