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

后端 未结 5 831
臣服心动
臣服心动 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 15:08

    Sort keys of NSDictionary based on dictionary keys

    Abobe is for returning a sorted array with based on dictionary content, this is for returning an array sorted by dictionary key:

    NSArray *keys = [theDictionary allKeys];
    NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        return [a compare:b];
    }];
    NSMutableArray *sortedValues = [NSMutableArray new];
    for(NSString *key in sortedKeys)
        [sortedValues addObject:[dictFilterValues objectForKey:key]];
    

提交回复
热议问题