Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2619569
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5921
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: iOS平台

2015-10-20 18:26:43

NSSortDescriptor使用注意以及直接排序字符串数组
http://blog.sina.com.cn/s/blog_8c87ba3b0101phr5.html

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,对数组本身排序。

阅读(794) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~