Welcome to my new blog: Developers say
Sorting an array of elements is a problem with any programming language, most of which have already provided some handy sorting methods for us to use, so what are the methods in iOS development that allow us to quickly sort the array elements?
First define an array and then sort the elements:
Nsarray *originalarray = [nsarray arraywithobjects: [nsnumbernumberwithint: 5],
[nsnumber numberwithint:6],
[nsnumber numberwithint:3],
[nsnumber numberwithint:4],
[nsnumber numberwithint:1],
[nsnumber numberwithint:2],
Nil];
Method 1:nscomparator
Nsarray *sortedarray = [Originalarray sortedarrayusingcomparator: ^ (nsnumber *number1, NSNumber *number2) {
int val1 = [number1 intvalue];
int val2 = [number2 intvalue];
if (Val1 > Val2) {
returnnsorderedascending;
} Else {
return nsordereddescending;
}
}];
NSLog (@ "%@", Sortedarray);
Method 2.Nssortdescriptor
Nssortdescriptor *descriptor = [[nssortdescriptoralloc]initwithkey:Nil Ascending:YES];
nsarray *sortarray = [nsarrayarraywithobjects:d escriptor,nil];
nsarray *sortedarray = [Originalarray Sortedarrayusingdescriptors:sortarray];
NSLog (@ "%@", Sortedarray);
Another: This blog will slowly stop the update, welcome to visit my new blog devsay.com for technical discussion.
iOS Array Quick Sort