如何对包含Mutable Dictionary的NSMutableArray进行排序?

时间:2013-10-28 12:00:07

标签: objective-c

我有一个NSMutableArray“arrayCompaniesData”在其中传递一个NSMutableDictionary,如[arrayCompaniesData addObject:mutableDic],在mutableDic中我有键emp_id,名称和描述我想用键'name'对这个mutableArray进行排序我怎么能这样做? ???我确实使用过NSSortDescriptor但是没有用.......

 NSArray *convertedArray_1 = [[NSArray alloc]initWithArray:arrayCompaniesData];
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"key" ascending:YES];
    NSArray *sortArray = [NSArray arrayWithObject:sort];
    NSArray *srt = [convertedArray_1 sortedArrayUsingDescriptors:sortArray];

1 个答案:

答案 0 :(得分:0)

最简单的方法可能是块

NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
    NSDictionary *first = (NSDictionary*)a;
    NSDictionary *second = (NSDictionary*)b;
    return [first[@"name"] compare:second[@"name"]];
}];