iOS sorting an array of objects to hold

Source: Internet
Author: User

Every program we develop uses some data, which is typically encapsulated in a custom class. For example a music program may have a song class, a chat program is another friend class, a la carte program will have a recipe class and so on. Sometimes we want the list data displayed in the program to be arranged in a certain order, so we'll look at what methods in iOS can sort the objects in Nsarray. Here is the table of contents:

    • Kohiki
    • Sort by using Nscomparator
    • Sort by using Nsdescriptor
    • Sort by using Selector
Kohiki

The object we will sort on is a persion class, as defined below:

    1. @interface Person:nsobject
    2. @property (nonatomic, copy) NSString *name;
    3. @property (nonatomic, copy) NSString *surname;
    4. @property (nonatomic, strong) NSDate *dateofbirth;
    5. @end

And the array contains the following:

    1. Smith John 03/01/1984
    2. Andersen Jane 16/03/1979
    3. Clark Anne 13/09/1995
    4. Smith David 19/07/1981
    5. Johnson Rose 22/02/1989
Sort by using Nscomparator

Comparator actually uses a block object as a comparison operation. It is defined as follows:

    1. typedef nscomparisonresult (^nscomparator) (ID obj1, id obj2);

The above parameters (Obj1, obj2) are the objects we will be comparing. The block returns the result of the Nscomparisonresult type to represent the order of two objects.

To sort the entire array, you need to use Nsarray's Sortarrayusingcomparator: Method, as shown in the following code:

    1. Nsarray *sortedarray = [self.persons sortedarrayusingcomparator:^nscomparisonresult (person *p1, person *p2) {
    2. return [P1.surname compare:p2.surname];
    3. }];

The result of the final sort is as follows:

    1. Andersen Jane
    2. Clark Anne
    3. Johnson Rose
    4. Smith John
    5. Smith David
Sort by using Nsdescriptor

Sort descriptor can be used not only to sort arrays, but also to specify the sort of element in table view, and to sort the data returned by the fetch request in core data. The sort descriptor can be handy for sorting multiple keys in an array. Here's a look at how to sort our arrays surname, and then sort by name:

    1. Nssortdescriptor *firstdescriptor = [[Nssortdescriptor alloc] initwithkey:@"surname" Ascending:yes];
    2. Nssortdescriptor *seconddescriptor = [[Nssortdescriptor alloc] initwithkey:@"name" Ascending:yes];
    3. Nsarray *sortdescriptors = [Nsarray arraywithobjects:firstdescriptor, Seconddescriptor, nil];
    4. Nsarray *sortedarray = [Self.persons sortedarrayusingdescriptors:sortdescriptors];

The result of sorting the above code is as follows:

    1. Andersen Jane
    2. Clark Anne
    3. Johnson Rose
    4. Smith David
    5. Smith John
Sort by using Selector

In person, we can also define our own method for comparing two objects and using this method for array sorting. The comparator message is sent to each object in the numeric value and carries another object in the array as a parameter. The return result of the custom method is this: if the object itself is less than the object in the argument, it returns nsorederedascending, instead returns nsordereddescending, or nsorderedsame if it is equal. As shown in the following code:

    1. -(Nscomparisonresult) Compare: (Person *) Otherperson {
    2. return [Self.dateofbirth Compare:otherPerson.dateOfBirth];
    3. }

This method is defined in the person class and is used to sort the birthdays of a person.

The methods described above are designed to do the same thing: sort the array, and you might want to choose which one to use instead. When you need to sort by multiple keys, the simplest way is to use the sort descriptor. If the comparison method is complex, it is advisable to use the selector outside of your own. Block is a powerful feature introduced after iOS 4, using block as a comparison, you can do a simple comparison without using any variables, but you can also define a complex block to replace selector.

Original http://mobile.51cto.com/hot-434804.htm

iOS sorting an array of objects to hold

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.