Object-C, NSArraySortTest, and array sorting

Source: Internet
Author: User

Object-C, NSArraySortTest, and array sorting

When I come back in the evening, I will continue to write the Object-C example. Today I am not planning to write the iOS visual interface program, so I am too tired.

Just now, dady called again, and he was bored.

In fact, I have always been saying that no matter what happens, don't be unhappy all day.

To live happily, you must cherish your life.

 

I have just written an array sorting statement. There are three methods: the compare method of the object, the comparison function, and the anonymous function-code block.

I personally think that there are two methods in essence.

One is the compareTo method that implements the Comparable interface, similar to the objects in Java.

One is that the input Comparator is flexible.

 

Code

 

/// Main. m // NSArraySort // Created by fansunion on 15/12/2. // Copyright (c) 2015 demo. All rights reserved. // # import
 
  
// Before defining the main function, it must be the same as the C language, and different from java. // compare the function, and sort NSComparisonResult intSort (id a, id B, void * context) in descending order) {int v1 = [a intValue]; int v2 = [B intValue]; if (v1> v2) {return NSOrderedAscending;} else if (v1 <v2) {return NSOrderedDescending ;} else {return NSOrderedSame;} int main (int argc, const char * argv []) {@ autoreleasepool {// string array, sorted using the compare method that comes with it. Equivalent to the compareTo method for implementing the Comparable interface of objects in Java NSArray * strArray = @ [@ "Java", @ "Android", @ "Object-C"]; strArray = [strArray sortedArrayUsingSelector: @ selector (compare :)]; NSLog (@ ("% @"), strArray); // an integer array, passed in a comparison function. It is equivalent to passing in a Comparator object in Java. This method is more flexible. NSArray * numberArray = @ [[NSNumber numberWithInteger: 2015], [NSNumber numberWithInt: 1949], [NSNumber numberWithInt: 2020]; numberArray = [numberArray identifier: intSort context: nil]; NSLog (@ "% @", numberArray); // use a code block comparison to sort data (which is slightly different from the 2nd method). NSArray * numberArrayAsc = [numberArray sortedArrayUsingComparator in ascending order: ^ (id a, id B) {int v1 = [a intValue]; int v2 = [B intValue]; if (v1> v2) {return NSOrderedDescending ;} else if (v1 <v2) {return NSOrderedAscending;} else {return NSOrderedSame ;}]; NSLog (@ "% @", numberArrayAsc);} return 0 ;}
 


 

Program output

 

20:39:09. 500 NSArraySort [5534: 380948] (

Android,

Java,

"Object-C"

)

20:39:09. 502 NSArraySort [5534: 380948] (

2020,

2015,

1949

)

20:39:09. 502 NSArraySort [5534: 380948] (

1949,

2015,

2020

)

Program ended with exit code: 0


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.