Objective C中数组排序几种情况的总结
大体上,OC中常用的数组排序有以下几种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors:
1、简单排序(sortedArrayUsingSelector:)
如果只是对字符串的排序,可以利用sortedArrayUsingSelector:方法就可以了,代码如下
1 //简单排序 2 void sortArray1(){ 3 NSArray *array = [NSArray arrayWithObjects:@"abc",@"456",@"123",@"789",@"ef", nil]; 4 NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(compare:)]; 5 NSLog(@"排序后:%@",sortedArray); 6 }
当然,除了利用字符串自带的compare:方法,也可以自己写compare:方法,进行对象的比较;如下:
首先是新建了Person类,实现方法如下(头文件就省了):
1 #import "Person.h" 2 @implementation Person 3 4 //直接实现静态方法,获取带有name和age的Person对象 5 +(Person *)personWithAge:(int) age withName:(NSString *)name{ 6 Person *person = [[Person alloc] init]; 7 person.age = age; 8 person.name = name; 9 return person; 10 } 11 12 //自定义排序方法 13 -(NSComparisonResult)comparePerson:(Person *)person{ 14 //默认按年龄排序 15 NSComparisonResult result = [[NSNumber numberWithInt:person.age] compare:[NSNumber numberWithInt:self.age]];//注意:基本数据类型要进行数据转换 16 //如果年龄一样,就按照名字排序 17 if (result == NSOrderedSame) { 18 result = [self.name compare:person.name]; 19 } 20 return result; 21 } 22 23 @end
主函数代码如下:
1 void sortArray2(){ 2 Person *p1 = [Person personWithAge:23 withName:@"zhangsan"]; 3 Person *p2 = [Person personWithAge:21 withName:@"lisi"]; 4 Person *p3 = [Person personWithAge:24 withName:@"wangwu"]; 5 Person *p4 = [Person personWithAge:24 withName:@"liwu"]; 6 Person *p5 = [Person personWithAge:20 withName:@"liwu"]; 7 NSArray *array = [NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil]; 8 NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(comparePerson:)]; 9 NSLog(@"排序后:%@",sortedArray); 10 }
2、利用block语法(sortedArrayUsingComparator:)
苹果官方提供了block语法,比较方便。其中数组排序可以用sortedArrayUsingComparator:方法,代码如下:
1 void sortArray3(){ 2 NSArray *array = [NSArray arrayWithObjects:@"1bc",@"4b6",@"123",@"789",@"3ef", nil]; 3 NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { 4 5 //这里的代码可以参照上面compare:默认的排序方法,也可以把自定义的方法写在这里,给对象排序 6 NSComparisonResult result = [obj1 compare:obj2]; 7 return result; 8 }]; 9 NSLog(@"排序后:%@",sortedArray); 10 }
3、高级排序(sortedArrayUsingDescriptors:)
如果是这样一种情况呢?Person类里有另外一个类的变量,比如说Person类除了name,age变量,还有一辆车Car类型,Car类里有个name属性。对Person对象进行排序,有这样的要求:按照Car的name排序,如果是同一辆车,也就是Car的name相同,那么再按照年龄进行排序,如果年龄也相同,最后按照Person的name进行排序。
上面这样就要使用第三种方法,利用排序描述器,不多说,有兴趣可以看看API介绍。代码如下:
首先写个Car类,实现类Car.m代码如下:
1 #import "Car.h" 2 @implementation Car 3 4 +(Car *)initWithName:(NSString *)name{ 5 Car *car = [Car alloc] init]; 6 car.name = name; 7 return car; 8 } 9 10 @end
然后改写Person类,实现类Person.m代码如下:
1 #import "Person.h" 2 #import "Car.h" 3 @implementation Person 4 5 +(Person *)personWithAge:(int)age withName:(NSString *)name withCar:(Car *)car{ 6 Person *person = [[Person alloc] init]; 7 person.age = age; 8 person.name = name; 9 person.car = car; 10 return person; 11 } 12 13 //这里重写description方法,用于最后测试排序结果显示 14 -(NSString *)description{ 15 return [NSString stringWithFormat:@"age is %zi , name is %@, car is %@",_age,_name,_car.name]; 16 } 17 18 @end
主函数代码如下:
1 void sortArray4(){ 2 //首先来3辆车,分别是奥迪、劳斯莱斯、宝马 3 Car *car1 = [Car initWithName:@"Audio"]; 4 Car *car2 = [Car initWithName:@"Rolls-Royce"]; 5 Car *car3 = [Car initWithName:@"BMW"]; 6 7 //再来5个Person,每人送辆车,分别为car2、car1、car1、car3、car2 8 Person *p1 = [Person personWithAge:23 withName:@"zhangsan" withCar:car2]; 9 Person *p2 = [Person personWithAge:21 withName:@"zhangsan" withCar:car1]; 10 Person *p3 = [Person personWithAge:24 withName:@"lisi" withCar:car1]; 11 Person *p4 = [Person personWithAge:23 withName:@"wangwu" withCar:car3]; 12 Person *p5 = [Person personWithAge:23 withName:@"wangwu" withCar:car2]; 13 14 15 //加入数组 16 NSArray *array = [NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil]; 17 18 //构建排序描述器 19 NSSortDescriptor *carNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"car.name" ascending:YES]; 20 NSSortDescriptor *personNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; 21 NSSortDescriptor *personAgeDesc = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES]; 22 23 //把排序描述器放进数组里,放入的顺序就是你想要排序的顺序 24 //我这里是:首先按照年龄排序,然后是车的名字,最后是按照人的名字 25 NSArray *descriptorArray = [NSArray arrayWithObjects:personAgeDesc,carNameDesc,personNameDesc, nil]; 26 27 NSArray *sortedArray = [array sortedArrayUsingDescriptors: descriptorArray]; 28 NSLog(@"%@",sortedArray); 29 }
结果如下:
从结果看出,先按照age排序,如果age相同,按照car排序,如果car相同,按照name排序。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。