First, from small to large sort
//**************************************************************************
1. Note the different methods used for variable arrays and non-variable groups (in fact, go directly to Nsarray and nsmutablearray the respective API to find)
Nsarray *myarray=[nsarray arraywithobjects:@ "5", @ "6", @ "1", @ "2", @ "3", @ "4", nil];
Nsmutablearray *mymutablearray=[nsmutablearray arraywithobjects:@ "5", @ "6", @ "1", @ "2", @ "3", @ "4", nil];
Nsarray *last=[myarray sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {
if ([Obj1 intvalue]>[obj2 intvalue]) {
return nsordereddescending;
}
else if ([Obj1 intvalue]<[obj2 intvalue])
{
return nsorderedascending;
}
Else
return nsorderedsame;
}];
NSLog (@ "last:%@", last);
[Mymutablearray sortusingcomparator:^nscomparisonresult (ID obj1, id obj2) {
if ([Obj1 intvalue]>[obj2 intvalue]) {
return nsordereddescending;
}
else if ([Obj1 intvalue]<[obj2 intvalue])
{
return nsorderedascending;
}
Else
return nsorderedsame;
}];
NSLog (@ "mymutable:%@", Mymutablearray);
//**************************************************************************
//**************************************************************************
2. Can be sorted by multiple keywords
Nsdictionary *dic1=[nsdictionary dictionarywithobjectsandkeys:@ "2", @ "id", @ "one", @ "age", nil];
Nsdictionary *dic2=[nsdictionary dictionarywithobjectsandkeys:@ "1", @ "id", @ "@", @ "age", nil];
Nsdictionary *dic3=[nsdictionary dictionarywithobjectsandkeys:@ "3", @ "id", @ "a", @ "age", nil];
Nsarray *array=[nsarray arraywithobjects:dic1,dic2,dic3, nil];
First by ID, then by age (ascending: Whether it is a larger order)
Nssortdescriptor *descriptor1=[nssortdescriptor sortdescriptorwithkey:@ "id" ascending:yes];
Nssortdescriptor *descriptor2=[nssortdescriptor sortdescriptorwithkey:@ "age" ascending:yes];
Nsarray *descriptorarray=[nsarray Arraywithobjects:descriptor1,descriptor2, nil];
Nsarray *lastarray=[nsarray Arraywitharray:[array Sortedarrayusingdescriptors:descriptorarray];
NSLog (@ "lastarray:%@", Lastarray);
//**************************************************************************
ios--Common class--nsarray