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