码迷,mamicode.com
首页 > 编程语言 > 详细

对数组中的字典进行排序

时间:2016-03-27 11:01:45      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

原来字典
NSArray *arr = @[@{@"index" : @"3", @"key" : @"value"}, @{@"index" : @"4", @"key" : @"value"}, @{@"index" : @"1", @"key" : @"value"}, @{@"index" : @"2", @"key" : @"value"}];

要重新按照字典里的index值排序变成这样

NSArray *arr = @[@{@"index" : @"1", @"key" : @"value"},
                 @{@"index" : @"2", @"key" : @"value"},
                 @{@"index" : @"3", @"key" : @"value"},
                 @{@"index" : @"4", @"key" : @"value"}];


方法一:
- (NSComparisonResult)compare:(Person *)otherObject {
    return [self.birthDate compare:otherObject.birthDate];
}
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];

方法二:(推荐)
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"
                                              ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];


方法三:
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSDate *first = [(Person*)a birthDate];
    NSDate *second = [(Person*)b birthDate];
    return [first compare:second];
}];
 
 

对数组中的字典进行排序

标签:

原文地址:http://www.cnblogs.com/Xujg/p/5325080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!