To sort a string array using Nssortdescriptor

Source: Internet
Author: User
Tags array sort sorted by name

Tag: des style ar color using SP strong on log

Nssortdescriptor Specifies the properties of the object used to sort the object array.

If the employee object needs to be sorted by name, the following descriptor is generated

Nssortdescriptor *descriptor = [Nssortdescriptor sortdescriptorwithkey:name Ascending:yes];

If you need more than one sort, such as sort by name first, then by date of entry. Then create a two descriptor

Nssortdescriptor *descriptor = [Nssortdescriptor sortdescriptorwithkey:hiredate Ascending:yes];

Two descriptor are placed in the array and passed to the array to be sorted.

If the object is NSString, that is, the string array sort, it is simpler, the Sortdescriptor key is directly specified as nil, directly sort the object, not one of the object's properties.

Nssortdescriptor *descriptor = [nssortdescriptor sortdescriptorwithkey:nil Ascending:yes];

Nsarray *descriptors = [Nsarray arraywithobject:descriptor];

Nsarray *mydataarray = [Nsarray arraywithobjects:@ "What", @ "Xero", @ "Highligth", @ "mountain", @ "Victory", @ "Balance", NIL];

Nsarray *resultarray = [Mydataarray sortedarrayusingdescriptors:descriptors];

NSLog (@ "%@", resultarray);

Nsarray uses sortedarrayusingdescriptors to return the sorted array.

Nsmutablearray can be used to sort the array itself directly using Sortusingdescriptors.

To sort a string array using Nssortdescriptor

Related Article

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.