Since the use of the collection, often need to sort the array operation, this blog summarizes several ways for an array ordered in the OC
1. When an object is stored in the array Foundation framework provide, directly compare: Method
Such as: NSString, NSMutableSting etc.
1 // used to sort the array block 2 NSArray * ARR = @ [ @ " . 4 " , @ " . 3 " , @ " 1 " , @ " 2 " ]; . 3 NSMutableArray mutArr * = [ARR mutableCopy]; . 4 . 5 // array using variable block sortUsingComparator . 6 [mutArr sortUsingComparator: ^ NSComparisonResult ( ID OBJ1, ID obj2) { . 7 return [OBJ1 Compare: obj2]; . 8 }]; . 9 // not set variables using ordered Comparator 10 NSArray * the sorted = [ARR sortedArrayUsingComparator: ^ NSComparisonResult ( ID OBJ1, ID obj2) { . 11 return [obj2 Compare: OBJ1]; 12 is }]; 13 is 14 NSLog ( @ " % @ " , the sorted); 15 NSLog ( @ " % @ " , mutArr); 16 . 17 // used to sort the array block 18 // variable selector array using the sort . 19 NSLog ( @" % @ " , [ARR sortedArrayUsingSelector: @selector (Compare :)]);
2. When the array is stored in a custom object, you need to write your own sorting method, sort or NSSortDescriptor
For example, a custom Student class, generating many Student object, into an array, the array is sorted.
2.1 Student category
1 #import <Foundation/Foundation.h> 2 @class Grade; 3 @interface Student : NSObject 4 @property (copy, nonatomic) NSString* name; 5 @property (assign, nonatomic) NSInteger age; 6 @property (copy, nonatomic) NSString* stuNo; 7 @property (strong, nonatomic) Grade* grade; 8 - (NSComparisonResult)compare:(Student *)stu; 9 10 - (NSString *)description; 11 12 @end 13 14 @implementation Student 15 - (NSComparisonResult)compare:(Student *)stu 16 { 17 return [_name compare: [stu name]]; 18 } 19 - (NSString *)description 20 { 21 return [NSString stringWithFormat:@"%@", _name]; 22 } 23 @end
2.2 sorting (using custom method)
1 // custom sort objects 2 Grade grade1 * = [[Grade the alloc] the init]; . 3 grade1.name = @ " 1023 " ; . 4 Grade grade2 * = [[Grade the alloc] the init]; . 5 grade2.name = @ " 1024 " ; . 6 . 7 Student STU1 * = [[Student the alloc] the init]; . 8 stu1.name = @ " Monkey " ; . 9 stu1.stuNo = @ " 008 " ; 10 stu1.age = 10 ; 11 stu1.grade = grade1; 12 Student* stu2 = [[Student alloc] init]; 13 stu2.name = @"八戒"; 14 stu2.stuNo = @"007"; 15 stu2.age = 12; 16 stu2.grade = grade2; 17 Student* stu3 = [[Student alloc] init]; 18 stu3.name = @"唐僧"; 19 stu3.stuNo = @"009"; 20 stu3.age = 14; 21 stu3.grade = grade2; 22 Student* stu4 = [[Student alloc] init]; 23 stu4.name = @"玉帝"; 24 stu4.stuNo = @"011"; 25 stu4.age = 16; 26 stu4.grade = grade1; 27 Student* stu5 = [[Student alloc] init]; 28 stu5.name = @"观音"; 29 stu5.stuNo = @"112 " ; 30 stu5.age = 14 ; 31 is stu5.grade = grade1; 32 33 is NSArray * Students. = @ [STU1, STU2, stu3, stu4, stu5]; 34 is // custom objects need to customize the sorted ordering 35 * = orderedArr NSArray [Students. sortedArrayUsingSelector: @selector (Compare :)]; 36 // NSLog (@ "% @", orderedArr); 37 [ for (Student * new new in orderedArr) { 38 is NSLog ( @ " % @ " , new new .name); 39 }
3. NSSortDescriptor sort, NSSortDescriptor more sorting conditions for sorting multiple
Steps for usage:
1> Create Object NSSortDescriptor
1 // Use NSSortDescriptor an object description of a property comparison rules 2 // The first parameter is the name of a member variable to the property in the comparison 3 // second parameter specifies to YES, showing ascending, designated is NO, showing descending . 4 . 5 NSSortDescriptor * desc1 = [[NSSortDescriptor the alloc] initWithKey: @ " _stuNo " Ascending: YES]; . 6 NSSortDescriptor * desc2 = [[NSSortDescriptor the alloc] initWithKey: @ " _age " Ascending: YES]; . 7 * = desc3 NSSortDescriptor [[NSSortDescriptor the alloc] initWithKey: @ " the _name " Ascending: YES]; . 8 //[Note] NSSortDescriptor support path, _grade student object property of the object, name attribute is _grade . 9 NSSortDescriptor desc4 * = [[NSSortDescriptor the alloc] initWithKey: @ " _grade.name " Ascending: YES];
2> The objects into an array NSSortDescriptor
1 NSArray* descArr = [NSArray arrayWithObjects: desc4,desc1, desc3, desc2, nil];
3> need to sort an array of calling sortedArrayUsingDescriptors: sorting method
1 NSArray* sortedByDescriptor = [students sortedArrayUsingDescriptors: descArr];
Example code:
Student.h file
1 #import <Foundation/Foundation.h> 2 @class Grade; 3 @interface Student : NSObject 4 @property (copy, nonatomic) NSString* name; 5 @property (assign, nonatomic) NSInteger age; 6 @property (copy, nonatomic) NSString* stuNo; 7 @property (strong, nonatomic) Grade* grade; 8 - (NSComparisonResult)compare:(Student *)stu; 9 10 - (NSString *)description; 11 12 @end
Student.m file
1 #import "Student.h" 2 3 @implementation Student 4 - (NSComparisonResult)compare:(Student *)stu 5 { 6 return [_name compare: [stu name]]; 7 } 8 - (NSString *)description 9 { 10 return [NSString stringWithFormat:@"%@", _name]; 11 } 12 @end
Grade.h file
1 #import <Foundation/Foundation.h> 2 3 @interface Grade : NSObject 4 @property (copy, nonatomic) NSString* name; 5 @end
Grade.m file
1 #import "Grade.h" 2 3 @implementation Grade 4 5 @end
The main function:
1 // custom sort objects 2 Grade grade1 * = [[Grade the alloc] the init]; . 3 grade1.name = @ " 1023 " ; . 4 Grade grade2 * = [[Grade the alloc] the init]; . 5 grade2.name = @ " 1024 " ; . 6 . 7 Student STU1 * = [[Student the alloc] the init]; . 8 stu1.name = @ " Monkey " ; . 9 stu1.stuNo = @ " 008 " ; 10 stu1.age = 10 ; 11 stu1.grade = grade1; 12 Student* stu2 = [[Student alloc] init]; 13 stu2.name = @"八戒"; 14 stu2.stuNo = @"007"; 15 stu2.age = 12; 16 stu2.grade = grade2; 17 Student* stu3 = [[Student alloc] init]; 18 stu3.name = @"唐僧"; 19 stu3.stuNo = @"009"; 20 stu3.age = 14; 21 stu3.grade = grade2; 22 Student* stu4 = [[Student alloc] init]; 23 stu4.name = @"玉帝"; 24 stu4.stuNo = @"011"; 25 stu4.age = 16; 26 stu4.grade = grade1; 27 Student* stu5 = [[Student alloc] init]; 28 stu5.name = @"观音"; 29 stu5.stuNo = @"112"; 30 stu5.age = 14; 31 stu5.grade = grade1; 32 33 NSArray* students = @[stu1, stu2, stu3, stu4,stu5]; 34 35 NSSortDescriptor* desc1 = [[NSSortDescriptor alloc] initWithKey:@"_stuNo" ascending:YES]; 36 NSSortDescriptor* desc2 = [[NSSortDescriptor alloc] initWithKey:@"_age" ascending:YES]; 37 NSSortDescriptor* desc3 = [[NSSortDescriptor alloc] initWithKey:@"_name" ascending:YES]; 38 //可向下传递 39 NSSortDescriptor* desc4 = [[NSSortDescriptor alloc] initWithKey:@"_grade.name" ascending:YES]; 40 NSArray* descArr = [NSArray arrayWithObjects: desc4,desc1, desc3, desc2, nil]; 41 // NSArray* descArr = [NSArray arrayWithObjects:desc4, nil]; 42 NSArray* sortedByDescriptor = [students sortedArrayUsingDescriptors: descArr]; 43 44 for (Student *new in sortedByDescriptor) { 45 NSLog(@"%@", new.grade.name); 46 }
Reproduced in: https: //www.cnblogs.com/pretty-guy/p/3996374.html