IOS 数组排序方法

//第一种,利用数组的sortedArrayUsingComparator调用 NSComparator ,obj1和obj2指的数组中的对象
NSComparator cmptr = ^(id obj1, id obj2){
 if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }
 
    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
};

NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前
NSMutableString *outputBefore = [[NSMutableString alloc] init];
for(NSString *str in sortArray){
	[outputBefore appendFormat:@"];
}
NSLog(@"排序前:%@",outputBefore);
//排序后
NSArray *array = [sortArray sortedArrayUsingComparator:cmptr];
NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
	[outputAfter appendFormat:@"];
}
NSLog(@"排序后:%@",outputAfter);

第二种 排序方法 利用sortedArrayUsingFunction 调用 对应方法customSort,这个方法中的obj1和obj2分别是指数组中的对象。
NSInteger customSort(id obj1, id obj2,void* context){
if ([obj1 integerValue] > [obj2 integerValue]) {
    return (NSComparisonResult)NSOrderedDescending;
}

if ([obj1 integerValue] < [obj2 integerValue]) {
    return (NSComparisonResult)NSOrderedAscending;
}
return (NSComparisonResult)NSOrderedSame;
}

NSArray *sortArray = [[NSArray alloc] initWithObjects:@"1",@"3",@"4",@"7",@"8",@"2",@"6",@"5",@"13",@"15",@"12",@"20",@"28",@"",nil];
//排序前
NSMutableString *outputBefore = [[NSMutableString alloc] init];
	for(NSString *str in sortArray){
	[outputBefore appendFormat:@"];
}
NSLog(@"排序前:%@",outputBefore);

NSArray *array = [sortArray sortedArrayUsingFunction:customSort context:nil];

NSMutableString *outputAfter = [[NSMutableString alloc] init];
for(NSString *str in array){
	[outputAfter appendFormat:@"];
}
NSLog(@"排序后:%@",outputAfter);

第三种 利用sortUsingDescriptors调用NSSortDescriptor

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"price" 
															   ascending:NO];//其中,price为数组中的对象的属性,这个针对数组中存放对象比较更简洁方便
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[_totalInfoArray sortUsingDescriptors:sortDescriptors];
[_airListView refreshTable:_totalInfoArray];

来源:http://hi.baidu.com/ivan_xu/item/5d897c14d51bffe0ddeeca7c

字符串的比较模式:

NSComparator cmptr = ^(id obj1, id obj2){
    if([[NSString stringWithFormat:@"%@",obj1] compare:[NSString stringWithFormat:@"%@",obj2] options:NSNumericSearch] > 0)
    {
        return (NSComparisonResult)NSOrderedDescending;
    }
    
    if([[NSString stringWithFormat:@"%@",obj1] compare:[NSString stringWithFormat:@"%@",obj2] options:NSNumericSearch] < 0)
    {
        return (NSComparisonResult)NSOrderedAscending;
    }

    return (NSComparisonResult)NSOrderedSame;
};

数字比较模式:


NSInteger customSort(id obj1, id obj2,void* context){
    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }
     
    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}




MY  Demo:

    NSComparator cmptr = ^( QualityQuantityInfoEntityEx * obj1, QualityQuantityInfoEntityEx * obj2)
    {
        if ([obj1 date] > [obj2 date])
        {
            return (NSComparisonResult)NSOrderedDescending;
        }


        if ([obj1 date] < [obj2 date])
        {
            return (NSComparisonResult)NSOrderedAscending;
        }


        return (NSComparisonResult)NSOrderedSame;
    };
    _qqEntityEx = data;
   _dataArray = (NSMutableArray*)[_qqEntityEx.quaList sortedArrayUsingComparator:cmptr];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值