IOS _ Objective-C _ array sorting, ios_objective-c
Generally, arrays in OC are sorted by sortedArrayUsingSelector:; sortedArrayUsingComparator:; sortedArrayUsingDescriptors :.
1. sortedArrayUsingSelector :)You can use sortedArrayUsingSelector to sort strings. The Code is as follows:
// Simple sorting
1 void sortArray1 () {2 3 NSArray * array = [NSArrayarrayWithObjects: @ "abc", @ "456", @ "123", @ "789", @ "ef ", nil]; 4 5 NSArray * sortedArray = [array sortedArrayUsingSelector: @ selector (compare :)]; 6 7 NSLog (@ "after sorting: % @", sortedArray); 8 9}
Of course, in addition to using the compare: method that comes with the string, you can also write the compare: Method to compare objects. For example: first, the Person class is created, the implementation method is as follows (the header file is saved ):
1 # import "Person. h" 2 3 @ implementation Person
7 // directly implement the static method to obtain the Person object with name and age 8 9 + (Person *) personWithAge :( int) age withName :( NSString *) name {10 11 Person * person = [[Person alloc] init]; 12 13 person. age = age; 14 15 person. name = name; 16 17 return person; 18 19} 20 23 // custom sorting method 24 25-(NSComparisonResult) comparePerson :( Person *) person {26 27 // sort by age by default 28 29 NSComparisonResult result = [[NSNumber numberWithInt: person. age] compare: [NSNumber numberWithInt: self. age]; // note: the basic data type needs to be converted to 30 31 // if the age is the same, the data is sorted by name 32 33 if (result = NSOrderedSame) {34 35 result = [self. name compare: person. name]; 36 37} 38 39 return result; 40 41} 47 @ end
The main function code is as follows:
1 void sortArray2 () {2 3 Person * p1 = [Person personWithAge: 23 withName: @ "zhangsan"]; 4 5 Person * p2 = [Person personWithAge: 21 withName: @ "lisi"]; 6 7 Person * p3 = [Person personWithAge: 24 withName: @ "wangwu"]; 8 9 Person * p4 = [Person personWithAge: 24 withName: @ "liwu"]; 10 11 Person * p5 = [Person personWithAge: 20 withName: @ "liwu"]; 12 13 NSArray * array = [NSArray arrayWithObjects: p1, p2, p3, p4, p5, nil]; 14 15 NSArray * sortedArray = [array sortedArrayUsingSelector: @ selector (comparePerson :)]; 16 17 NSLog (@ "after sorting: % @", sortedArray); 18 19}
2. Use the block syntax (sortedArrayUsingComparator :)The block syntax provided by Apple is more convenient. SortedArrayUsingComparator can be used to sort arrays. The Code is as follows:
1 void sortArray3 () {2 3 NSArray * array = [NSArrayarrayWithObjects: @ "1bc", @ "4b6", @ "123", @ "789", @ "3ef ", nil]; 4 5 NSArray * sortedArray = [array sortedArrayUsingComparator: ^ NSComparisonResult (id obj1, id obj2) {8 9 // refer to the above compare: Default sorting method, you can also write the custom method here to sort the objects 10 11 NSComparisonResult = [obj1 compare: obj2]; 12 13 return result; 14 15}]; 16 17 NSLog (@ "after sorting: % @", sortedArray); 18 19}
3. sortedArrayUsingDescriptors :)What if this is the case? The Person class has another class variable. For example, in addition to the name and age variables, the Person class also has a Car type, and the Car class has a name attribute. Sort Person objects by Car name. If the name of a Car is the same, the Person objects are sorted by age, if the age is the same, sort by Person name. In this case, we need to use the third method, using the sort descriptor. If you are interested, please refer to the API introduction. The Code is as follows: first write a Car class, and the Implementation class Car. m code is as follows:
#import "Car.h"@implementation Car
+(Car *)initWithName:(NSString *)name{ Car *car = [[Car alloc] init]; car.name = name; return car;}@end
Then rewrite the Person class to implement the Person. m Code as follows:
1 # import "Person. h "2 3 # import" Car. h "4 5 @ implementation Person 8 9 + (Person *) personWithAge :( int) age withName :( NSString *) name withCar :( Car *) car {10 11 Person * person = [[Person alloc] init]; 12 13 person. age = age; 14 15 person. name = name; 16 17 person. car = car; 18 19 return person; 20 21}
25 // here, the description method is rewritten to display 26 27-(NSString *) description {28 29 return [NSString stringWithFormat: @ "age is % zi, name is % @, car is % @ ", _ age, _ name, _ car. name]; 30 31} 32 33 34 @ end
The main function code is as follows:
1 void sortArray4 () {2 3 // first, three vehicles are available: Audi, Rolls-Royce, and BMW 4 5 Car * car1 = [Car initWithName: @ "Audio"]; 6 7 Car * car2 = [Car initWithName: @ "Rolls-Royce"]; 8 9 Car * car3 = [Car initWithName: @ "BMW"]; 12 13 // five more persons, each of which delivers a car, namely car2, car1, car1, car3, car214 15 Person * p1 = [Person personWithAge: 23 withName: @ "zhangsan" withCar: car2]; 16 17 Person * p2 = [Person personWithAge: 21 withName: @ "zhangsan" withCar: car1]; 18 19 Person * p3 = [Person personWithAge: 24 withName: @ "lisi" withCar: car1]; 20 21 Person * p4 = [Person personWithAge: 23 withName: @ "wangwu" withCar: car3]; 22 23 Person * p5 = [Person personWithAge: 23 withName: @ "wangwu" withCar: car2]; 28 29 // Add array 30 31 NSArray * array = [NSArray arrayWithObjects: p1, p2, p3, p4, p5, nil]; 34 35 // construct the sorting descriptor 36 37 NSSortDescriptor * carNameDesc = [NSSortDescriptor sortDescriptorWithKey: @ "car. name "ascending: YES]; 38 39 NSSortDescriptor * personNameDesc = [NSSortDescriptor restart: @" name "ascending: YES]; 40 41 NSSortDescriptor * personAgeDesc = [NSSortDescriptor restart: @ "age" ascending: YES]; 44 45 // put the sort descriptor in the array, and put the order in which you want to sort 46 47 // here I am: first, sort by age, then the car name, and finally by person name 48 49 NSArray * descriptorArray = [NSArrayarrayWithObjects: personAgeDesc, carNameDesc, personNameDesc, nil];
53 NSArray * sortedArray = [array sortedArrayUsingDescriptors: descriptorArray]; 54 55 NSLog (@ "% @", sortedArray); 56 57}
The result is as follows: sort by age first. If the age is the same, sort by car. If the car is the same, sort by name. (Note: The above two sorting methods to achieve string display, Please rewrite the description method) original blog address: http://850361034.blog.163.com/blog/static/32803809201436111445914/
1 ThanksJust Have A Try