大体上,OC中常用的数组排序有以下几种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors:。
1、简单排序(sortedArrayUsingSelector:)
如果只是对字符串的排序,可以利用sortedArrayUsingSelector:方法就可以了,代码如下
//简单排序
1voidsortArray1(){23NSArray *array = [NSArrayarrayWithObjects:@"abc",@"456",@"123",@"789",@"ef", nil];45NSArray *sortedArray =[array sortedArrayUsingSelector:@selector(compare:)];67NSLog(@"排序后:%@",sortedArray);89}
当然,除了利用字符串自带的compare:方法,也可以自己写compare:方法,进行对象的比较;如下:
首先是新建了 Person 类,实现方法如下(头文件就省了):
1#import"Person.h"23@implementationPerson
7//直接实现静态方法,获取带有name和age的Person对象89+(Person *)personWithAge:(int) age withName:(NSString *)name{1011Person *person =[[Person alloc] init];1213person.age =age;1415person.name =name;1617returnperson;1819}2023//自定义排序方法2425-(NSComparisonResult)comparePerson:(Person *)person{2627//默认按年龄排序2829NSComparisonResult result = [[NSNumber numberWithInt:person.age]compare:[NSNumber numberWithInt:self.age]];//注意:基本数据类型要进行数据转换3031//如果年龄一样,就按照名字排序3233if(result ==NSOrderedSame) {3435result =[self.name compare:person.name];3637}3839returnresult;4041}47@end
主函数代码如下:
1voidsortArray2(){23Person *p1 = [Person personWithAge:23withName:@"zhangsan"];45Person *p2 = [Person personWithAge:21withName:@"lisi"];67Person *p3 = [Person personWithAge:24withName:@"wangwu"];89Person *p4 = [Person personWithAge:24withName:@"liwu"];1011Person *p5 = [Person personWithAge:20withName:@"liwu"];1213NSArray *array =[NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil];1415NSArray *sortedArray =[array sortedArrayUsingSelector:@selector(comparePerson:)];1617NSLog(@"排序后:%@",sortedArray);1819}
2、利用block语法(sortedArrayUsingComparator:)
苹果官方提供了block语法,比较方便。其中数组排序可以用sortedArrayUsingComparator:方法,代码如下:
1voidsortArray3(){23NSArray *array = [NSArrayarrayWithObjects:@"1bc",@"4b6",@"123",@"789",@"3ef", nil];45NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(idobj1,idobj2) {89//这里的代码可以参照上面compare:默认的排序方法,也可以把自定义的方法写在这里,给对象排序1011NSComparisonResult result =[obj1 compare:obj2];1213returnresult;1415}];1617NSLog(@"排序后:%@",sortedArray);1819}
3、高级排序(sortedArrayUsingDescriptors:)
如果是这样一种情况呢?Person类里有另外一个类的变量,比如说Person类除了name,age变量,还有一辆车Car类型,Car类里有个name属性。对Person对象进行排序,有这样的要求:按照Car的name排序,如果是同一辆车,也就是Car的name相同,那么再按照年龄进行排序,如果年龄也相同,最后按照Person的name进行排序。
上面这样就要使用第三种方法,利用排序描述器,不多说,有兴趣可以看看API介绍。代码如下:
首先写个Car类,实现类Car.m代码如下:
#import"Car.h"@implementationCar
+(Car *)initWithName:(NSString *)name{
Car*car =[[Car alloc] init];
car.name=name;returncar;
}@end
然后改写Person类,实现类Person.m代码如下:
1#import"Person.h"23#import"Car.h"45@implementationPerson89+(Person *)personWithAge:(int)age withName:(NSString *)name withCar:(Car *)car{1011Person *person =[[Person alloc] init];1213person.age =age;1415person.name =name;1617person.car =car;1819returnperson;2021}
25//这里重写description方法,用于最后测试排序结果显示2627-(NSString *)description{2829return[NSString stringWithFormat:@"age is %zi , name is %@, car is %@",_age,_name,_car.name];3031}323334@end
主函数代码如下:
1voidsortArray4(){23//首先来3辆车,分别是奥迪、劳斯莱斯、宝马45Car *car1 = [Car initWithName:@"Audio"];67Car *car2 = [Car initWithName:@"Rolls-Royce"];89Car *car3 = [Car initWithName:@"BMW"];1213//再来5个Person,每人送辆车,分别为car2、car1、car1、car3、car21415Person *p1 = [Person personWithAge:23withName:@"zhangsan"withCar:car2];1617Person *p2 = [Person personWithAge:21withName:@"zhangsan"withCar:car1];1819Person *p3 = [Person personWithAge:24withName:@"lisi"withCar:car1];2021Person *p4 = [Person personWithAge:23withName:@"wangwu"withCar:car3];2223Person *p5 = [Person personWithAge:23withName:@"wangwu"withCar:car2];2829//加入数组3031NSArray *array =[NSArray arrayWithObjects:p1,p2,p3,p4,p5, nil];3435//构建排序描述器3637NSSortDescriptor *carNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"car.name"ascending:YES];3839NSSortDescriptor *personNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"name"ascending:YES];4041NSSortDescriptor *personAgeDesc = [NSSortDescriptor sortDescriptorWithKey:@"age"ascending:YES];4445//把排序描述器放进数组里,放入的顺序就是你想要排序的顺序4647//我这里是:首先按照年龄排序,然后是车的名字,最后是按照人的名字4849NSArray *descriptorArray =[NSArrayarrayWithObjects:personAgeDesc,carNameDesc,personNameDesc, nil];
53NSArray *sortedArray =[array sortedArrayUsingDescriptors: descriptorArray];5455NSLog(@"%@",sortedArray);5657}
结果如下:
从结果看出,先按照age排序,如果age相同,按照car排序,如果car相同,按照name排序。
(注意:上面两种排序方法要想实现字符串显示,请重写description方法)
原博客地址: http://850361034.blog.163.com/blog/static/32803809201436111445914/
1Thanks
Just Have A Try