大体上,OC中常用的数组排序有以下几种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors:。
//简单排序
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(@"排序后:%@",sortedArray);8 9 }
1 #import "Person.h" 2 3 @implementation Person
7 //直接实现静态方法,获取带有name和age的Person对象 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 //自定义排序方法24 25 -(NSComparisonResult)comparePerson:(Person *)person{26 27 //默认按年龄排序28 29 NSComparisonResult result = [[NSNumber numberWithInt:person.age]compare:[NSNumber numberWithInt:self.age]];//注意:基本数据类型要进行数据转换30 31 //如果年龄一样,就按照名字排序32 33 if (result == NSOrderedSame) {34 35 result = [self.name compare:person.name];36 37 }38 39 return result;40 41 }47 @end
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(@"排序后:%@",sortedArray);18 19 }
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 //这里的代码可以参照上面compare:默认的排序方法,也可以把自定义的方法写在这里,给对象排序10 11 NSComparisonResult result = [obj1 compare:obj2];12 13 return result;14 15 }];16 17 NSLog(@"排序后:%@",sortedArray);18 19 }
#import "Car.h"@implementation Car
+(Car *)initWithName:(NSString *)name{ Car *car = [[Car alloc] init]; car.name = name; return car;}@end
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 //这里重写description方法,用于最后测试排序结果显示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
1 void sortArray4(){ 2 3 //首先来3辆车,分别是奥迪、劳斯莱斯、宝马 4 5 Car *car1 = [Car initWithName:@"Audio"]; 6 7 Car *car2 = [Car initWithName:@"Rolls-Royce"]; 8 9 Car *car3 = [Car initWithName:@"BMW"];12 13 //再来5个Person,每人送辆车,分别为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 //加入数组30 31 NSArray *array = [NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil];34 35 //构建排序描述器36 37 NSSortDescriptor *carNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"car.name" ascending:YES];38 39 NSSortDescriptor *personNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];40 41 NSSortDescriptor *personAgeDesc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];44 45 //把排序描述器放进数组里,放入的顺序就是你想要排序的顺序46 47 //我这里是:首先按照年龄排序,然后是车的名字,最后是按照人的名字48 49 NSArray *descriptorArray = [NSArrayarrayWithObjects:personAgeDesc,carNameDesc,personNameDesc, nil];
53 NSArray *sortedArray = [array sortedArrayUsingDescriptors: descriptorArray];54 55 NSLog(@"%@",sortedArray);56 57 }
1 Thanks
新闻热点
疑难解答