ios数组(NSArray)的排序

排序使用的是NSArray类别里面的方法

- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors;    // returns a new array by sorting the objects of the receiver

注意是返回一个新的array,所以必须要创建一个新的对象去保存它,不然,排序完的对象只是一个临时变量,原array本身没有发生改变。

+ (instancetype)sortDescriptorWithKey:(NSString *)key ascending:(BOOL)ascending NS_AVAILABLE(10_6, 4_0);

根据对象的变量属性进行简单的排序,需要使用到的是NSSortDescriptor的数组集合,可以单个也可以多个,当第一条件相同时候根据第二条件排序,一次类推,

ascendingYes为递增排序,ascending为No递减排序。

下面是一个自己定义的Object的排序,sort具体内容下面再放出


    NSArray *dataArr = @[sortObject0,sortObject1,sortObject2,sortObject3,sortObject4];

    NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
    NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
    NSSortDescriptor *sortDescriptor3 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];


//根据年龄递增排序

    NSArray *firstArr = [_dataArr sortedArrayUsingDescriptors:@[sortDescriptor1]];

//根据年龄递减排序

    NSArray *secondArr = [_dataArr sortedArrayUsingDescriptors:@[sortDescriptor2]];

//年龄递增排序,年龄相同时,按名称字母顺序排序
    NSArray *thirdArr = [_dataArr sortedArrayUsingDescriptors:@[sortDescriptor1,sortDescriptor3]];

//年龄递减排序,年龄相同时,按名称字母顺序排序
    NSArray *fouthArr = [_dataArr sortedArrayUsingDescriptors:@[sortDescriptor2,sortDescriptor3]];


sortObject的代码

@interface NHSortObject : NSObject

+ (id)sortObjectWithName:(NSString *)name WithAge:(int)age WithHeight:(float)height;

- (void)setObjectName:(NSString *)name;
- (void)setObjectAge:(int)age;
- (void)setObjectHeight:(float)height;

- (void)logObject;

@end

 ----------

@implementation NHSortObject
{
    //设置3个变量属性
    
    NSString *_name;
    int _age;
    float _height;
}

//静态方法调用构造函数

+ (id)sortObjectWithName:(NSString *)name WithAge:(int)age WithHeight:(float)height
{
    NHSortObject *sortObject = [[NHSortObject alloc]init];
    
    [sortObject setObjectName:name];
    [sortObject setObjectAge:age];
    [sortObject setObjectHeight:height];
    
    return sortObject;
}

- (void)setObjectName:(NSString *)name
{
    _name = name;
}

- (void)setObjectAge:(int)age
{
    _age = age;
}

- (void)setObjectHeight:(float)height
{
    _height = height;
}

//打印变量数据
- (void)logObject
{
    NSLog(@" ");
    NSLog(@"name %@,age %d,height %f",_name,_age,_height);
}

@end

---------- 

//实例化五个对象
    
    NHSortObject *sortObject0 = [NHSortObject sortObjectWithName:@"zhangsan" WithAge:24 WithHeight:1.75];
    NHSortObject *sortObject1 = [NHSortObject sortObjectWithName:@"adi" WithAge:19 WithHeight:1.70];
    NHSortObject *sortObject2 = [NHSortObject sortObjectWithName:@"liwu" WithAge:25 WithHeight:1.80];
    NHSortObject *sortObject3 = [NHSortObject sortObjectWithName:@"liwu" WithAge:24 WithHeight:1.90];
    NHSortObject *sortObject4 = [NHSortObject sortObjectWithName:@"lining" WithAge:8 WithHeight:1.90];


注意这里的age为int,身高为float,这样系统会判断数值的大小,如果age为NSString,8的值是大于24的,因为NSString的排序是逐位对比排序的,对比了8>2就结束了,不会判断8<24的,如果使用Dic储存数值的话,记得用NsNumber不要用NSString。

附上log出来的数据


2015-04-20 17:40:32.181 NHSortApp[3028:151082]  
2015-04-20 17:40:32.210 NHSortApp[3028:151082] name zhangsan,age 24,height 1.750000
2015-04-20 17:40:32.210 NHSortApp[3028:151082]  
2015-04-20 17:40:32.210 NHSortApp[3028:151082] name adi,age 19,height 1.700000
2015-04-20 17:40:32.210 NHSortApp[3028:151082]  
2015-04-20 17:40:32.210 NHSortApp[3028:151082] name liwu,age 25,height 1.800000
2015-04-20 17:40:32.211 NHSortApp[3028:151082]  
2015-04-20 17:40:32.211 NHSortApp[3028:151082] name liwu,age 24,height 1.900000
2015-04-20 17:40:32.211 NHSortApp[3028:151082]  
2015-04-20 17:40:32.211 NHSortApp[3028:151082] name lining,age 8,height 1.900000
2015-04-20 17:40:32.230 NHSortApp[3028:151082]  
2015-04-20 17:40:32.230 NHSortApp[3028:151082] fitst sort age
2015-04-20 17:40:32.230 NHSortApp[3028:151082]  
2015-04-20 17:40:32.230 NHSortApp[3028:151082] name lining,age 8,height 1.900000
2015-04-20 17:40:32.231 NHSortApp[3028:151082]  
2015-04-20 17:40:32.231 NHSortApp[3028:151082] name adi,age 19,height 1.700000
2015-04-20 17:40:32.231 NHSortApp[3028:151082]  
2015-04-20 17:40:32.231 NHSortApp[3028:151082] name zhangsan,age 24,height 1.750000
2015-04-20 17:40:32.231 NHSortApp[3028:151082]  
2015-04-20 17:40:32.231 NHSortApp[3028:151082] name liwu,age 24,height 1.900000
2015-04-20 17:40:32.391 NHSortApp[3028:151082]  
2015-04-20 17:40:32.391 NHSortApp[3028:151082] name liwu,age 25,height 1.800000
2015-04-20 17:40:32.391 NHSortApp[3028:151082]  
2015-04-20 17:40:32.391 NHSortApp[3028:151082] second sort
2015-04-20 17:40:32.391 NHSortApp[3028:151082]  
2015-04-20 17:40:32.392 NHSortApp[3028:151082] name liwu,age 25,height 1.800000
2015-04-20 17:40:32.392 NHSortApp[3028:151082]  
2015-04-20 17:40:32.392 NHSortApp[3028:151082] name zhangsan,age 24,height 1.750000
2015-04-20 17:40:32.392 NHSortApp[3028:151082]  
2015-04-20 17:40:32.392 NHSortApp[3028:151082] name liwu,age 24,height 1.900000
2015-04-20 17:40:32.392 NHSortApp[3028:151082]  
2015-04-20 17:40:32.392 NHSortApp[3028:151082] name adi,age 19,height 1.700000
2015-04-20 17:40:32.393 NHSortApp[3028:151082]  
2015-04-20 17:40:32.393 NHSortApp[3028:151082] name lining,age 8,height 1.900000
2015-04-20 17:40:32.407 NHSortApp[3028:151082]  
2015-04-20 17:40:32.407 NHSortApp[3028:151082] third sort
2015-04-20 17:40:32.407 NHSortApp[3028:151082]  
2015-04-20 17:40:32.407 NHSortApp[3028:151082] name lining,age 8,height 1.900000
2015-04-20 17:40:32.408 NHSortApp[3028:151082]  
2015-04-20 17:40:32.408 NHSortApp[3028:151082] name adi,age 19,height 1.700000
2015-04-20 17:40:32.408 NHSortApp[3028:151082]  
2015-04-20 17:40:32.408 NHSortApp[3028:151082] name liwu,age 24,height 1.900000
2015-04-20 17:40:32.408 NHSortApp[3028:151082]  
2015-04-20 17:40:32.408 NHSortApp[3028:151082] name zhangsan,age 24,height 1.750000
2015-04-20 17:40:32.408 NHSortApp[3028:151082]  
2015-04-20 17:40:32.408 NHSortApp[3028:151082] name liwu,age 25,height 1.800000


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值