Objective-C: Sort keys of NSDictionary based on dictionary entries

后端 未结 5 816
臣服心动
臣服心动 2021-02-13 14:30

OK so I know that dictionaries can\'t be sorted. But, say I have NSMutableArray *keys = [someDictionary allKeys]; Now, I want to sort those keys, based on the corre

5条回答
  •  有刺的猬
    2021-02-13 14:52

    NSArray *keys = [someDictionary allKeys];
    NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSString *first = [someDictionary objectForKey:a];
        NSString *second = [someDictionary objectForKey:b];
        return [first compare:second];
    }];
    

提交回复
热议问题