原来字典
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];
}];
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步