假设我有一个包含6个字典的数组 - 每个字典都有一个“位置”键,其中包含相关值。我怎样才能将这些价值观排在一起,这样我才能从中得到......
"location" : USA
"location" : UK
"location" : Asia
"location" : UK
"location" : USA
"location" : Asia
到此?
"location" : Asia
"location" : Asia
"location" : UK
"location" : UK
"location" : USA
"location" : USA
答案 0 :(得分:0)
我想sortedArrayUsingComparator:
方法就是您要找的方法:
NSArray *array = @[@{@"location" : @"USA"},
@{@"location" : @"UK"},
@{@"location" : @"Asia"},
@{@"location" : @"UK"},
@{@"location" : @"USA"},
@{@"location" : @"Asia"}];
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [[obj1 valueForKey:@"location"] compare:[obj2 valueForKey:@"location"]];
}];