Explanation of nsarray sorting method

Source: Internet
Author: User

Explanation of nsarray sorting method

There are multiple ways to sort Arrays

The most troublesome thing issortedArrayUsingSelector:, FollowedsortedArrayUsingDescriptors:, The easiest to use issortedArrayUsingComparator:

Start with the easiest way to use:

// Original array nsarray * array = @ [@ "B", @ "A", @ "X", @ "O", @ "g ", @ "O"]; // sorting array nsarray * sort = [array sortedarrayusingcomparator: ^ nscomparisonresult (ID obj1, Id obj2) {nsstring * str1 = obj1; nsstring * str2 = obj2; return [str1 compare: str2];}]; // print the sorting array [sort enumerateobjectsusingblock: ^ (id obj, nsuinteger idx, bool * stop) {nslog (@ "% @", OBJ) ;}];

It is too easy to solve this problem.

To sort the objects, use the corresponding objects to receive them :)

Is it too simple.

Remember, block sorting is the easiest way!

Next trysortedArrayUsingDescriptors:This method.

sortedArrayUsingDescriptors:It is generally used to sort the model, and the block can also sort the model. The definition of the model is given first (do not be too lazy to read the tutorial, just tap the code yourself)

The following is the Sorting code:

//// Appdelegate. M // sort // http://www.cnblogs.com/YouXianMing///// copyright (c) 2014 y. x. all rights reserved. // # import "appdelegate. H "# import" model. H "@ implementation appdelegate-(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {self. window = [[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds]; [Self sort]; self. window. backgroundcolor = [uicolor whitecolor]; [self. window makekeyandvisible]; return yes;}-(void) Sort {// original array nsarray * array = @ [model name: @ "youxianming" Age: @ 26 height: 171], [model name: @ "Xiaoqiu" Age: @ 27 Height: 170], [model name: @ "haoqushi" Age: @ 28 Height: 172], [model name: @ "jungang" Age: @ 24 Height: 171], [model name: @ "Kongming" Age: @ 30 Height: 175], [model name: @ "gaofushuai" Age: @ 22 Height: 180]; // nssortdescriptor * sortdescriptor = [[nssortdescriptor alloc] initwithkey: @ "name" ascending: Yes]; nsarray * sortdescriptors = [nsarray sequence: sortdescriptor]; nsarray * sortedarray = [array sequence: sortdescriptors]; // print the sorting information [sortedarray sequence: ^ (id obj, nsuinteger idx, bool * Stop) {model * TMP = OBJ; nslog (@ "% @", TMP. name) ;}] ;}@ end

Look, actually, nssortdescriptor is just a tool to get the keypath. It can sort it according to the keypath. That's all :)

Take a look at the printed information:

09:09:43. 563 sort [86442: 60b] gaofushuai
09:09:43. 565 sort [86442: 60b] haoqushi
09:09:43. 565 sort [86442: 60b] jungang
09:09:43. 566 sort [86442: 60b] Kongming
09:09:43. 566 sort [86442: 60b] Xiaoqiu
09:09:43. 567 sort [86442: 60b] youxianming

Very easy.

This kind of stuff is encapsulated into a category.

Usage:

//// Appdelegate. M // sort // http://www.cnblogs.com/YouXianMing///// copyright (c) 2014 y. x. all rights reserved. // # import "appdelegate. H "# import" model. H "# import" nsarray + yxsort. H "@ implementation appdelegate-(bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions {self. window = [[uiwindow alloc] initwithframe: [[uiscreen mainscreen] bounds]; [Self sort]; self. window. backgroundcolor = [uicolor whitecolor]; [self. window makekeyandvisible]; return yes;}-(void) Sort {// original array nsarray * array = @ [model name: @ "youxianming" Age: @ 26 height: 171], [model name: @ "Xiaoqiu" Age: @ 27 Height: 170], [model name: @ "haoqushi" Age: @ 28 Height: 172], [model name: @ "jungang" Age: @ 24 Height: 171], [model name: @ "Kongming" Age: @ 30 Height: 175], [model name: @ "gaofushuai" Age: @ 22 Height: 180]; // sort nsarray * sortedarray = [array sortedwithkeypath: @ "name" ascending: Yes]; // print the sorting information [sortedarray enumerateobjectsusingblock: ^ (id obj, nsuinteger idx, bool * Stop) {model * TMP = OBJ; nslog (@ "% @", TMP. name) ;}] ;}@ end

One sentence can achieve sorting, much simpler :), developers have to hide unnecessary red lines and reduce unnecessary interference.

Method 3sortedArrayUsingSelector:Maybe it's the most common method you use. I don't want to talk about it anymore. I think it's too troublesome. I have to write another comparison method ......

 

 

Summary:

= I prefer this =

1. Prioritize block sorting

2. sort by nssortdescriptor's keypath

3. Try againsortedArrayUsingSelector:Sort by Method

 

Appendix:

It is as simple and straightforward to sort models with blocks. You only need to use the model to receive objects.

 

 

 

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.