ios--Common class--nsarray

Source: Internet
Author: User

First, from small to large sort

//**************************************************************************

1. Note the different methods used for variable arrays and non-variable groups (in fact, go directly to Nsarray and nsmutablearray the respective API to find)

Nsarray *myarray=[nsarray arraywithobjects:@ "5", @ "6", @ "1", @ "2", @ "3", @ "4", nil];

Nsmutablearray *mymutablearray=[nsmutablearray arraywithobjects:@ "5", @ "6", @ "1", @ "2", @ "3", @ "4", nil];

Nsarray *last=[myarray sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {

if ([Obj1 intvalue]>[obj2 intvalue]) {

return nsordereddescending;

}

else if ([Obj1 intvalue]<[obj2 intvalue])

{

return nsorderedascending;

}

Else

return nsorderedsame;

}];

NSLog (@ "last:%@", last);

[Mymutablearray sortusingcomparator:^nscomparisonresult (ID obj1, id obj2) {

if ([Obj1 intvalue]>[obj2 intvalue]) {

return nsordereddescending;

}

else if ([Obj1 intvalue]<[obj2 intvalue])

{

return nsorderedascending;

}

Else

return nsorderedsame;

}];

NSLog (@ "mymutable:%@", Mymutablearray);

//**************************************************************************

//**************************************************************************

2. Can be sorted by multiple keywords

Nsdictionary *dic1=[nsdictionary dictionarywithobjectsandkeys:@ "2", @ "id", @ "one", @ "age", nil];

Nsdictionary *dic2=[nsdictionary dictionarywithobjectsandkeys:@ "1", @ "id", @ "@", @ "age", nil];

Nsdictionary *dic3=[nsdictionary dictionarywithobjectsandkeys:@ "3", @ "id", @ "a", @ "age", nil];

Nsarray *array=[nsarray arraywithobjects:dic1,dic2,dic3, nil];

First by ID, then by age (ascending: Whether it is a larger order)

Nssortdescriptor *descriptor1=[nssortdescriptor sortdescriptorwithkey:@ "id" ascending:yes];

Nssortdescriptor *descriptor2=[nssortdescriptor sortdescriptorwithkey:@ "age" ascending:yes];

Nsarray *descriptorarray=[nsarray Arraywithobjects:descriptor1,descriptor2, nil];

Nsarray *lastarray=[nsarray Arraywitharray:[array Sortedarrayusingdescriptors:descriptorarray];

NSLog (@ "lastarray:%@", Lastarray);

//**************************************************************************

ios--Common class--nsarray

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.