"Learning the path of iOS: Objective-c" The system ordering method commonly used in OC

Source: Internet
Author: User

Common sorting methods in ①.oc:

1). Non-variable group

-(Nsarray *) Sortedarrayusingselector: (SEL) comparator;-(Nsarray *) Sortedarrayusingcomparator: (NSComparator) cmptr;
2) Variable Group

-(void) Sortusingselector: (SEL) comparator;-(void) Sortusingcomparator: (Nscomparator) cmptr;
3). Dictionary sort

-(Nsarray *) Keyssortedbyvalueusingcomparator: (Nscomparator) Cmptr-(Nsarray *) Keyssortedbyvalueusingselector: (SEL) Comparator
② Applications

1). Array of Immutable Variables: (Method 1)

   Nsarray *arr = @[@ "AA", @ "RR", @ "pp", @ "hh", @ "xx", @ "VV"];   The system lacks the method of two element comparison to sort by the system method.   The Selector method selector.        Nsarray *sortarr = [arr sortedarrayusingselector: @selector (compare:)];    NSLog (@ "%@", Sortarr);   
Method 2:block Block syntax

   [Arr sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2) {        return [(NSString *) obj1 compare: (nsstring *) obj2];    }];     NSLog (@ "%@", arr);    }
2). Variable Array Ordering: Method 1

    Nsmutablearray *arr = [@[@54, @33,@12,@23,@65] mutablecopy];    [Arr sortusingselector: @selector (Compare:) method    for comparing two elements in an array];//compare NSLog (@ "%@", arr);
Method 2
   [Arr sortusingcomparator:^nscomparisonresult (ID obj1, id obj2) {        return [(NSNumber *) obj1 Compare: (NSNumber *) obj2 ];    }];     NSLog (@ "%@", arr);    }
Note: The dictionary method is similar


③. Example: Define a Student object, according to the student object, name, age, score, number of students to sort, (two sorts of ways)

Method 1: (with the Selector method selector, you need to redefine the comparator)

The code is as follows:

1. Object declaration (in student.h)

@property (nonatomic, retain) nsstring *name; @property (nonatomic, assign) Nsinteger age; @property (nonatomic, assign) CG Float score; @property (nonatomic, assign) Nsinteger number;//initialization-(ID) Initwithname: (NSString *) name Age: (Nsinteger) Age Score:                                  (cgfloat) score number: (Nsinteger) number;//convenience Builder + (ID) studentwithname: (NSString *) name Age: (Nsinteger) Age Score:                                   (cgfloat) score number: (Nsinteger) number;//Two students according to the method of comparison-(Nscomparisonresult) Comparebyage: (Student *) stu;//two students by name comparison Method-(Nscomparisonresult) Comparebyname: (Student *) stu;//two students according to the method of comparison-(Nscomparisonresult) Comparebyscore: (Student *) stu;//two students in accordance with the method of the number comparison-(Nscomparisonresult) Comparebynumber: (Student *) Stu;
2. Implementation (STUDENT.M file)
 -(ID) Initwithname: (NSString *) Name: (Nsinteger) Age Score: (cgfloat) score Numbe     R: (nsinteger) Number {self = [super init];         if (self! = nil) {self.name = name;         Self.age = age;         Self.score = score;     Self.number = number; } return self;} Convenience builder + (ID) studentwithname: (NSString *) name Age: (Nsinteger) Age Score: (CGFloat) SCO                                      Re number: (Nsinteger) Number {Student *student = [[Student alloc] Initwithname:name       age:age Score:score Number:number]; return student;} Rewrite description-(NSString *) Description {return [NSString stringWithFormat: @ "Name:%@,age:%ld,socre :%.1f,number:%ld ", Self.name, Self.age, Self.score, Self.number];} Two students by age comparison Method-(Nscomparisonresult) Comparebyage: (Student *) Stu {   return [@ (self.age) compare:@ (stu.age )];//or the following method    return self.age > Stu.age? Nsordereddescending  : self.age = = Stu.age? nsorderedsame:nsorderedascending;} Two students by name descending method-(Nscomparisonresult) Comparebyname: (Student *) Stu {    return-[Self.name compare: Stu.name];} Two students in descending order of grades-(Nscomparisonresult) Comparebyscore: (Student *) Stu {return-[@ (Self.score) Compare: (Stu.score)];& nbsp;  //return Self.score < Stu.score? Nsordereddescending:  self.score = = Stu.score? nsorderedsame:nsorderedascending;     } //Two students follow the method of ascending the number of learners-(Nscomparisonresult) Comparebynumber: (Student *) stu {return [@ (Self.number) Compare: (Stu.number)];   //return self.num ber > Stu.number? NSOrderedDescending:self.number = = Stu.number? nsorderedsame:nsorderedascending;}

Main function (Main.h)

Student *student1 = [Student studentwithname:@ "a" age:23 score:21 number:3434343]; Student *student2 = [Student studentwithname:@ "B" age:45 score:432.4 number:324]; Student *student3 = [Student studentwithname:@ "C" age:32 score:4321.4 number:2343]; Student *student4 = [Student studentwithname:@ "D" Age:7 score:43.4 number:233];        Student *student5 = [Student studentwithname:@ "E" age:73 score:65 number:2332424]; Nsmutablearray *arr = [Nsmutablearray arraywithobjects:student1,student2,student3,student4,stud                Ent5,nil];  Sort by age Ascending [arr sortusingselector: @selector (comparebyage:)];  NSLog (@ "%@", arr);  Sort by grade descending [arr sortusingselector: @selector (comparebyscore:)];  NSLog (@ "%@", arr);  Sort by name descending [arr sortusingselector: @selector (comparebyname:)];  NSLog (@ "%@", arr);  Sort by number Ascending [arr sortusingselector: @selector (comparebynumber:)]; NSLog (@ "%@", arr);
Method 2. Sorting with block blocks syntax

       Sort by age in ascending order        [arr sortusingcomparator:^ (ID obj1,id obj2) {          return [@ (((Student *) obj1). Ages) Compare: @ (((Student * ) (OBJ2). Age)];        }];        NSLog (@ "%@", arr);                Sort by score Descending        [arr sortusingcomparator:^ (ID obj1,id obj2) {      return [@ (((Student *) obj1). Score) Compare: @ (((stude NT *) obj2). Score)];        }];                NSLog (@ "%@", arr);        Sort by name descending        [arr sortusingcomparator:^ (ID obj1,id obj2) {           return-[[(Student *) obj2 name] Compare: [(Student *) o Bj1 name] [];        }];        NSLog (@ "%@", arr);                Sort by number ascending        Nscomparator Sortblock = ^ (id obj1,id obj2) {           return [@ (((Student *) obj1). No.) Compare: @ ((( Student *) obj2)];        };        [Arr Sortusingcomparator:sortblock];        NSLog (@ "%@", arr);



"Learning the path of iOS: Objective-c" The system ordering method commonly used in OC

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.