Foundation framework--sort instances of content in arrays

Source: Internet
Author: User

In the Person class

@property(nonatomic,strong)nsstring * name;

@property(nonatomic ,strong)nsstring *year;

@property(nonatomic,assign)int age ;

-(ID) initwithname: (nsstring *) name Andyear: (nsstring *) Year Andage: (int.) age;


person.m

-(nsstring *) description

{

return [nsstringstringwithformat:@ "name=%@,year= %@,age=%d ",_name,_year,_age];

}

Initialization

-(ID) initwithname: (nsstring *) name Andyear: (nsstring *) Year Andage: (int.) Age

{

if ( self =[superinit])

{

_name=name;

_year=year;

_age=age;

}


return self;

}


in the main function, do the following:

//1. The array is stored in the order in which the data is deposited.

nsarray *array=@[@ "B",@ "D",@ "1",@ "A",@ " Z ",@" 2 "];

NSLog (@ " sort before array%@", array);

array= [Arraysortedarrayusingselector:@selector(compare:)];

NSLog (@ " sort after array%@", array);

//2. Sort using block mode.

nsarray *array1=@[@ "z",@ "4",@ "3",@ "x"];

NSLog (@ "Array1 %@ before sorting ", array1);

Array1 =[array1sortedarrayusingcomparator: ^nscomparisonresult(ID obj1,ID obj2 ) {

Nscomparisonresult rersult=[obj1 Compare:obj2];

return rersult;

return [Obj1 Compare: Obj2];

}];

NSLog (@ "Array1 %@ after sorting ", array1);

//3. Sorting of custom objects, sorting by attributes

person *p=[[personalloc]initwithname: c10>@ "Jack"andyear:@ "1990"andage:];

        person *p1=[[ person alloc ] initwithname : @ "Tom" andyear : @ "" " andage: 21 ];

        person *p2=[[ person alloc ] initwithname : @ "Hoo" andyear: @ "1908" andage: 18 ];

person *p3=[[personalloc]initwithname: c8>@ "Hoo1"andyear:@ "1907"andage:];


nsarray *array2=@[p,p1,p2,p3];

NSLog (@ " sort before array2%@", array2);

        nssortdescriptor *d1=[ Nssortdescriptor sortdescriptorwithkey Span style= "COLOR: #000000" >: @ "Age" ascending : yes ];

nssortdescriptor *d2=[nssortdescriptorsortdescriptorwithkey:@ " Year "ascending:YES";

nsarray *descripts=@[d1,d2];

Array2 =[array2sortedarrayusingdescriptors:d escripts];

NSLog (@ " array2%@ after sorting ", array2);

//4. Block method Ordering for custom objects

nsarray *array4=@[p,p1,p2,p3];

NSLog (@ "Array4 %@ before sorting ", Array4);

Array4=[array4sortedarrayusingcomparator: ^nscomparisonresult(ID obj1,ID OBJ2) {

person *p1=obj1;

person *p2=obj2;

return [P1. Year Compare:p 2. Year];

}];

NSLog (@ "Array4 %@ after sorting ", Array4);




Foundation framework--sort instances of content in arrays

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.