The first, using the sortedarrayusingcomparator of an array, calls Nscomparator, Obj1, and Obj2 objects in the array
Nscomparator cmptr = ^ (ID obj1, id obj2) {
if ([Obj1 integervalue] > [obj2 integervalue]) {
Return (Nscomparisonresult) nsordereddescending;
}
if ([Obj1 integervalue] < [obj2 IntegerValue]) {
Return (Nscomparisonresult) nsorderedascending;
}
Return (Nscomparisonresult) nsorderedsame;
};
nsarray *sortArray = [[ Nsarray alloc] initwithobjects:@ "1", @ "3", @ "4", @ "7", @ "8", @ "2", @ "6", @ "5", @ "@", @ "@", @ "a", @ "," @ "" ", @" ", nil];
//before sorting
nsmutablestring *outputbefore = [[Nsmutablestring alloc] init];
for (NSString *str in Sortarray) {
[outputbefore appendformat:@ "];
}
nslog (@ "before sorting:%@", outputbefore);
[outputbefore release];
//First sort
nsarray *array = [Sortarray sortedarrayusingcomparator:cmptr];
nsmutablestring *outputafter = [[Nsmutablestring alloc] init];
for (NSString *str in array) {
[outputafter appendformat:@ "];
}
nslog (@ "After sorting:%@", outputafter);
[outputafter release];
the second sort method Using Sortedarrayusingfunction to call the corresponding method Customsort, the obj1 and Obj2 in this method are the objects in the exponential group respectively.
Nsinteger customsort (ID obj1, ID obj2,void* context) {
if ([obj1 integervalue] > [obj2 IntegerValue]) {
return (nscomparisonresult) nsordereddescending;
if ([Obj1 integervalue] < [obj2 IntegerValue]) {
return (Nscomparisonresult) nsorderedascending;
}
return (nscomparisonresult) nsorderedsame;
}
//before sorting
nsmutablestring *outputbefore = [[Nsmutablestring alloc] init];
for (NSString *str in Sortarray) {
[outputbefore appendformat:@ "];
}
nslog (@ "before sorting:%@", outputbefore);
[outputbefore release];
nsarray *array = [Sortarray sortedarrayusingfunction:customsort context:nil];
nsmutablestring *outputafter = [[Nsmutablestring alloc] init];
for (NSString *str in array) {
[outputafter appendformat:@ "];
}
nslog (@ "After sorting:%@", outputafter);
[outputafter release];
The third is the use of Sortusingdescriptors call Nssortdescriptor
Nssortdescriptor *sortdescriptor = [[Nssortdescriptor alloc] initwithkey:@ "Price" ascending:no];//where the price is the property of an object in the array , this is more concise and convenient for storing objects in an array
Nsarray *sortdescriptors = [[Nsarray alloc] Initwithobjects:&sortdescriptor count:1];
[_totalinfoarray sortusingdescriptors:sortdescriptors];
[_airlistview Refreshtable:_totalinfoarray];
[Sortdescriptor release];
[Sortdescriptors release];
Several simple and efficient array sorting methods for iOS