首页 > 代码库 > 关于NSArray的几种排序:

关于NSArray的几种排序:

利用数组的sortedArrayUsingComparator调用 NSComparator 

其中NSComparator其实就是一个返回NSComparisonResult的block。

         typedef NSComparisonResult (^NSComparator)(id obj1,id obj2); 其中obj1、obj2其实是NSArray中的元素

    resultArray = [arrayDic <span style="color:#009900;">sortedArrayUsingComparator:</span>^NSComparisonResult(id obj1, id obj2) {
        NSNumber * number1 = [[obj1 allKeys] objectAtIndex:0];
        NSNumber * number2 = [[obj2 allKeys] objectAtIndex:0];
        NSComparisonResult result = [number1 compare:number2];
        return result == NSOrderedAscending;
    }];

利用数组的sortedArrayUsingFunction 调用 对应方法customSort

NSInteger <span style="color:#ff9900;">sortByID</span>(id obj1, id obj2, void *context){
NSString *str1 =(NSString*) obj1; // ibj1 和 obj2 来自与你的数组中,其实,个人觉得是苹果自己实现了一个冒泡排序给大家使用
NSString *str2 =(NSString *) obj2;
if (str1.length < str2.length) {
<span style="white-space:pre">	</span>return NSOrderedDescending;
}
else if(str1.length == str2.length)
{
<span style="white-space:pre">	</span>return NSOrderedSame;
}
<span style="white-space:pre">	</span>return NSOrderedAscending;
}
NSArray *sortedArray =[arr sortedArrayUsingFunction:sortByID context:nil];

#利用数组的sortedArrayUsingSelector调用 对应的SEL的方法

注意selector的方法是对数组元素而言的方法,假如数据元素没有compare:方法,可以通过扩展数组元素的类增加相应的方法。      

    NSMutableArray *arrayDic = [NSMutableArray arrayWithObjects:
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj0", [NSNumber numberWithInt:0], nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj5", [NSNumber numberWithInt:5], nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj2", [NSNumber numberWithInt:2], nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj3", [NSNumber numberWithInt:3], nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj1", [NSNumber numberWithInt:1], nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:@"Obj4", [NSNumber numberWithInt:4], nil], nil];
#ifdef sortedArrayUsingSelector
    resultArray = [arrayDic sortedArrayUsingSelector:@selector(compare:)];

因为数组中元素对应的是字典,所以对字典的类进行扩展

@implementation NSDictionary (extend)
- (NSComparisonResult)compare: (NSDictionary *)otherDictionary
{
    NSNumber *number2 = [[otherDictionary allKeys] objectAtIndex:0];
    NSDictionary *tempDictionary = (NSDictionary *)self;
    NSNumber *number1 = [[tempDictionary allKeys] objectAtIndex:0];
    NSComparisonResult result = [number1 compare:number2];
    
//    return result == NSOrderedDescending; // 升序
      return result == NSOrderedAscending;  // 降序
}

#利用数组的sortUsingDescriptors调用NSSortDescriptor

NSSortDescriptor 可以简单的理解为  指定对象的某属性的比较描述。

    /**
     *  可以很方便的根据model对象中的某个属性进行排序
     *   sortDescriptor1 数组根据name进行升序排
     *   sortDescriptor2 数组根据age进行降序排
     *   同时将两种sortDescriptor加入数组,是指先按照name排序,name相同的按照age排序
     */
    Person *person1 = [[Person alloc] init];
    [person1 setName:@"ABC"];
    [person1 setAge:24];
    
    Person *person2 = [[Person alloc] init];
    [person2 setName:@"ACB"];
    [person2 setAge:22];
    
    Person *person3 = [[Person alloc] init];
    [person3 setName:@"ABD"];
    [person3 setAge:33];
    NSMutableArray *array = [NSMutableArray arrayWithObjects:person1, person2, person3, nil];
    NSSortDescriptor *sortDescriptor1 = [NSSortDescriptor <span style="color:#ff0000;">sortDescriptorWithKey</span>:@"_name" ascending:YES];
    NSSortDescriptor *sortDescriptor2 = [NSSortDescriptor <span style="color:#ff0000;">sortDescriptorWithKey</span>:@"_age" ascending:NO];
    resultArray = [array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sortDescriptor1,sortDescriptor2,nil]];
    for(NSInteger i = 0; i < [resultArray count]; i++)
    {
        NSLog(@"%@--------%d\n", [[resultArray objectAtIndex:i] name], [[resultArray objectAtIndex:i] age]);
    }