Several simple and efficient array sorting methods for iOS

Source: Internet
Author: User

The first, using the sortedarrayusingcomparator of an array, calls Nscomparator, Obj1, and Obj2 objects in the array

Nscomparator cmptr = ^ (ID obj1, id obj2) {
if ([Obj1 integervalue] > [obj2 integervalue]) {
Return (Nscomparisonresult) nsordereddescending;
}

if ([Obj1 integervalue] < [obj2 IntegerValue]) {
Return (Nscomparisonresult) nsorderedascending;
}
Return (Nscomparisonresult) nsorderedsame;
};

nsarray *sortArray = [[ Nsarray alloc] initwithobjects:@ "1", @ "3", @ "4", @ "7", @ "8", @ "2", @ "6", @ "5", @ "@", @ "@", @ "a", @ "," @ "" ", @" ", nil];
 //before sorting
 nsmutablestring *outputbefore = [[Nsmutablestring alloc] init];
 for (NSString *str in Sortarray) {
  [outputbefore appendformat:@ "];
 }
 nslog (@ "before sorting:%@", outputbefore);
 [outputbefore release];
 //First sort
 nsarray *array = [Sortarray sortedarrayusingcomparator:cmptr];
 
 nsmutablestring *outputafter = [[Nsmutablestring alloc] init];
 for (NSString *str in array) {
  [outputafter appendformat:@ "];
 }
 nslog (@ "After sorting:%@", outputafter);
 [outputafter release];


the second sort method Using Sortedarrayusingfunction to call the corresponding method Customsort, the obj1 and Obj2 in this method are the objects in the exponential group respectively.
Nsinteger customsort (ID obj1, ID obj2,void* context) {
 if ([obj1 integervalue] > [obj2 IntegerValue]) {
        return (nscomparisonresult) nsordereddescending;
   
 
    if ([Obj1 integervalue] < [obj2 IntegerValue]) {
         return (Nscomparisonresult) nsorderedascending;
   }
    return (nscomparisonresult) nsorderedsame;
}

 //before sorting
 nsmutablestring *outputbefore = [[Nsmutablestring alloc] init];
 for (NSString *str in Sortarray) {
  [outputbefore appendformat:@ "];
 }
 nslog (@ "before sorting:%@", outputbefore);
 [outputbefore release];
 
 nsarray *array = [Sortarray sortedarrayusingfunction:customsort context:nil];
 
 nsmutablestring *outputafter = [[Nsmutablestring alloc] init];
 for (NSString *str in array) {
  [outputafter appendformat:@ "];
 }
 nslog (@ "After sorting:%@", outputafter);
 [outputafter release];


The third is the use of Sortusingdescriptors call Nssortdescriptor

Nssortdescriptor *sortdescriptor = [[Nssortdescriptor alloc] initwithkey:@ "Price" ascending:no];//where the price is the property of an object in the array , this is more concise and convenient for storing objects in an array
Nsarray *sortdescriptors = [[Nsarray alloc] Initwithobjects:&sortdescriptor count:1];
[_totalinfoarray sortusingdescriptors:sortdescriptors];
[_airlistview Refreshtable:_totalinfoarray];
[Sortdescriptor release];
[Sortdescriptors release];

Several simple and efficient array sorting methods for iOS

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.