The Nssortdescriptor of Objective-c grammar

Source: Internet
Author: User

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

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.