Objective-c Sortusingselector method

Source: Internet
Author: User
Tags sorted by name

Today we learn two methods, one is to sort the string, and the other is to sort the number of constants.

Create a person class, and in the Person.h file, set the instance initialization and the setter getter method to also declare the sorted method in the Person.h file.

#import <Foundation/Foundation.h>

@interface Person: nsobject

1. set three instances name sex age

{

nsstring *_name;

nsstring *_sex;

Nsinteger _age;

}

2. Specify initialization

-(ID) initwithname: (nsstring *) name

Sex: (nsstring *) Sex

Age: (Nsinteger) age;

3.setter Getter Method

-(void) SetName: (nsstring *) name;

-(NSString *) name;

-(void) Setsex: (nsstring *) sex;

-(NSString *) sex;

-(void) Setage: (nsinteger) age;

-(Nsinteger) age;

4. Declaring a method that is sorted by name

-(Nscomparisonresult) Comparebynamewithotherperson: (Person*) Anotherperson;

5. Declaring an age-ordered approach

-(Nscomparisonresult) Comparebyagewithotherperson: (person*) Anotherperson;

6. Declare a description

-(NSString *) description;

@end

The implementation of the method in the PERSON.M

After declaring it in the Person.h file, copy the 2.3.4.5.6 down to the person.m file for implementation.

#import "Person.h"

@implementation Person

2. Specify initialization

-(ID) initwithname: (nsstring *) name

Sex: (nsstring *) Sex

Age: (Nsinteger) age

{

Self = [super Init];

if (self) {

_name = name;

_sex = sex;

_age = age;

}

return self;

}

3.setter Getter Method

-(void) SetName: (nsstring *) name

{

_name = name;

}

-(NSString *) name

{

return _name;

}

-(void) Setsex: (nsstring *) Sex

{

_sex = sex;

}

-(NSString *) Sex

{

return _sex;

}

-(void) Setage: (nsinteger) Age

{

_age = age;

}

-(Nsinteger) age

{

return _age;

}

4. Declaring a method that is sorted by name

-(Nscomparisonresult) Comparebynamewithotherperson: (Person*) Anotherperson

{

if ([Self. Name Compare:anotherperson. Name] > 0) {

return nsordereddescending;

}Else if ([self]. Name Compare:anotherperson. Name] < 0)

{

return nsorderedascending;

}

return nsorderedsame;

}

5. Declaring an age-ordered approach

-(Nscomparisonresult) Comparebyagewithotherperson: (person*) Anotherperson

{

if (self. Age > Anotherperson. Age ) {

return nsordereddescending;

}Else if (self. Age < Anotherperson. Age )

{

return nsorderedascending;

}

return nsorderedsame;

}

6. Declare a description

-(NSString *) Description

{

return [nsstring stringwithformat: @ "name:%@, Sex:%@, age:%ld",_name,_sex ,_age];

}

@end

method declaration and implementation, we will jump to the main function to invoke the method

#import <Foundation/Foundation.h>

#import "Person.h"

int main (int argc, const char * argv[])

{

@autoreleasepool {

7. Create 4 objects

Person *obj1 = [[Person alloc] initwithname:@ ' Jack ' sex:@ ' male ' age :];

Person *obj2 = [[Person alloc] initwithname:@ ' Henry ' sex:@ ' male ' age: [];

Person *obj3 = [[Person alloc] initwithname:@ "Elyse" sex:@ "female" Age:+];

Person *obj4 = [[Person alloc] initwithname:@ ' Lisa ' sex:@ ' female ' age: ];

Nsmutablearray *array = [[Nsmutablearray alloc] initwithobjects: obj1,obj2,obj3,obj4, Nil ];

8. array sorted by name

[Array sortusingselector:@selector (Comparebynamewithotherperson:)];

//Call Comparebyname This method , at run time , will first jump to Person.h , see if there is no this method , found , will jump to PERSON.M , run the method , and then jump to the main function and display the results .

NSLog (@ "%@", array);

9. array sorted by age

[Array sortusingselector:@selector (Comparebyagewithotherperson:)];

//Call Comparebyage This method , in the run time , will first jump to Person.h , see if there is this method , found , will jump to person.m , run the method , and then jump to the main function and display the results .

NSLog (@ "%@", array);

}

return 0;

}

This time the main is to sort the string and constant number of the master, should be used in the future, so the method to remember!

Objective-c Sortusingselector method

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.