NSSortDescriptor(数组排序)
来源:互联网 发布:网络语言爸爸什么意思 编辑:程序博客网 时间:2024/04/19 20:39
如果数组里面的每一个元素都是一个个model,例如
DepartsDate.h文件
#import <Foundation/Foundation.h>@interface DepartsDate : NSObject@property (nonatomic, retain) NSDate *date;@property (nonatomic, assign) int price;@endDepartsDate.m文件
#import "DepartsDate.h"@implementation DepartsDate@synthesize date, price;- (void)dealloc{ [date release]; [super dealloc];}@end那么对这个数组排序就可以这样
NSMutableArray *array = [NSMutableArray array];......NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];[array sortUsingDescriptors:[NSArray arrayWithObject:sort]];这是按照时间升序排列。如果需要降序,那么将YES改为NO。
使用NSSortDescriptor可以很方便的进行多条件排序,例如:同样是上面的假设,首先按照价格升序排列;当价格相同时,按照日期降序排列。
NSMutableArray *array = [NSMutableArray array];......NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES];NSSortDescriptor *sort2 = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO];[array sortUsingDescriptors:[NSArray arrayWithObjects:sort1, sort2, nil]];
版权声明:请大家转载的时候写上作者---周敏。
NSSortDescriptor仅对NSSet,NSArray,NSMutableArray可以排序,需要注意的是
NSSet- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors;这是对NSSet排序,返回一个NSArray。NSArray- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors;这是对NSArray排序,返回一个新的NSArray,注意内存泄漏。NSMutableArray- (void)sortUsingDescriptors:(NSArray *)sortDescriptors;这是NSMutableArray排序,没有返回值。 0 0
- NSSortDescriptor(数组排序)
- NSSortDescriptor(数组排序)
- NSSortDescriptor(数组排序)
- NSSortDescriptor(数组排序)
- NSSortDescriptor(数组排序)
- NSSortDescriptor(数组排序)
- NSSortDescriptor(数组排序)
- NSSortDescriptor(数组排序)
- 数组排序NSSortDescriptor
- NSSortDescriptor(数组排序)
- NSSortDescriptor(数组排序)
- NSSortDescriptor(数组排序)
- NSSortDescriptor(数组排序)
- NSSortDescriptor使用以及数组排序
- 利用 NSSortDescriptor 对数组进行排序
- NSSortDescriptor排序
- NSSortDescriptor排序
- NSSortDescriptor使用注意以及直接排序字符串数组
- hdu2391(简单dp)
- LeetCode 76 Minimum Window Substring
- 二、ROS相关网站汇总
- word2010文档中的网址链接显示的是乱码
- win8.1+vs2012环境下装boost1.55
- NSSortDescriptor(数组排序)
- SAPI 包含sphelper.h编译错误解决方案
- c语言运算符号
- 第九周项目5
- 数据仓库数据挖掘——数据仓库模型设计
- NIO的理解
- 1.Linux基础操作:主题2 Shell的基本概念
- Hibernate关联关系配置(一对多、一对一和多对多)
- (四)模板方法学习