iOS Array Quick Sort

Source: Internet
Author: User


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

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.