OC方法之---NSSortDescriptor使用注意以及直接排序字符串数组
2015-12-16 10:54
736 查看
NSSortDescriptor 指定用于对象数组排序的对象的属性。
如果是Employee对象需要按照name来排序,就生成下面的descriptor
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:name ascending:YES];
如果需要多个排序,比如先按name排序,再按入职日期排序。那就创建两个descriptor
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:hireDate ascending:YES];
两个descriptor放到数组里一起传给需要排序的数组。
如果对象就是NSString,就是字符串数组排序,那更简单了,sortdescriptor的key直接指定为nil,就直接排序对象,而不是对象的某一个属性了。
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:descriptor];
NSArray *myDataArray = [NSArray arrayWithObjects:@"what", @"xero", @"highligth", @"mountain",@"Victory", @"Balance", nil];
NSArray *resultArray = [myDataArray sortedArrayUsingDescriptors:descriptors];
NSLog(@"%@", resultArray);
NSArray 使用sortedArrayUsingDescriptors,返回排序好的数组。
NSMutableArray可以直接使用sortUsingDescriptors,对数组本身排序。
如果是Employee对象需要按照name来排序,就生成下面的descriptor
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:name ascending:YES];
如果需要多个排序,比如先按name排序,再按入职日期排序。那就创建两个descriptor
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:hireDate ascending:YES];
两个descriptor放到数组里一起传给需要排序的数组。
如果对象就是NSString,就是字符串数组排序,那更简单了,sortdescriptor的key直接指定为nil,就直接排序对象,而不是对象的某一个属性了。
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:descriptor];
NSArray *myDataArray = [NSArray arrayWithObjects:@"what", @"xero", @"highligth", @"mountain",@"Victory", @"Balance", nil];
NSArray *resultArray = [myDataArray sortedArrayUsingDescriptors:descriptors];
NSLog(@"%@", resultArray);
NSArray 使用sortedArrayUsingDescriptors,返回排序好的数组。
NSMutableArray可以直接使用sortUsingDescriptors,对数组本身排序。
相关文章推荐
- IOS 实现系统push和pop的动画
- [LeetCode]Triangle
- LIST<T>现在也支持序列化和反序列化了
- STM32F303X单片机USB例程详细解析4
- 获取对象信息
- ACCESS 里面的坑
- 通过PHP简单实例介绍文件上传
- 关于substring中 str>2?2:0的用法
- 时间管理&项目管理工具
- SQL Server 执行计划操作符详解(2)——串联(Concatenation )
- SQL Server 执行计划操作符详解(2)——串联(Concatenation )
- SQL Server 执行计划操作符详解(2)——串联(Concatenation )
- 模拟购物网站商品放大镜实现
- 微信企业号开发三:主动调用模式之发送news消息
- fopen()
- OC中NSMutableString的使用
- CentOS7 设置开机直接进入命令行界面
- 铁路工业旅游的大时代当开启
- Android音频系统之AudioFlinger(二)
- swift 学习笔记