您的位置:首页 > 其它

对数组中的字典进行排序

2016-03-27 10:29 302 查看
原来字典
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.birthDatecompare:otherObject.birthDate];
}
NSArray*sortedArray;
sortedArray=[drinkDetailssortedArrayUsingSelector:@selector(compare:)];

方法二:(推荐)

NSSortDescriptor*sortDescriptor;
sortDescriptor=[[[NSSortDescriptoralloc]initWithKey:@"birthDate"
ascending:YES]autorelease];
NSArray*sortDescriptors=[NSArrayarrayWithObject:sortDescriptor];
NSArray*sortedArray;
sortedArray=[drinkDetailssortedArrayUsingDescriptors:sortDescriptors];

方法三:

NSArray*sortedArray;
sortedArray=[drinkDetailssortedArrayUsingComparator:^NSComparisonResult(ida,idb){
NSDate*first=[(Person*)abirthDate];
NSDate*second=[(Person*)bbirthDate];
return[firstcompare:second];
}];



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: