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

后端 未结 5 828
臣服心动
臣服心动 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];
    }];
    
    0 讨论(0)
  • 2021-02-13 14:58
    NSDictionary *dict = // however you obtain the dictionary
    NSMutableArray *sortedKeys = [NSMutableArray array];
    
    NSArray *objs = [dict allValues];
    NSArray *sortedObjs = [objs sortedArrayUsingSelector:@selector(compare:)];
    for (NSString *s in sortedObjs)
        [sortedKeys addObjectsFromArray:[dict allKeysForObject:s]];
    

    Now sortedKey will contain the keys sorted by their corresponding objects.

    0 讨论(0)
  • 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]];
    
    0 讨论(0)
  • 2021-02-13 15:08

    If any value from the dictionary is null then use this code

    NSArray *keys = [typeDict allKeys];   
    NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
    NSArray *sortedKeys = [keys sortedArrayUsingDescriptors:@[sd]];
    NSLog(@"SORT 1 %@",sortedKeys);
    
    NSMutableDictionary *sortedTypeDict = [NSMutableDictionary dictionary];
    int counter = 0;
    for(int i=0; i < [typeDict count]; i++){
        id val = [typeDict objectForKey:[sortedKeys objectAtIndex:counter]];
        NSString *thekey = [sortedKeys objectAtIndex:counter];
        [sortedTypeDict setObject:val forKey:thekey];
        counter++;
    }
    NSLog(@"\n\nsorted dict: %@", sortedTypeDict);
    
    0 讨论(0)
  • 2021-02-13 15:16

    Just write it here cause I didn't find it anywhere: To get an alphanumeric sorted NSDictionary back from an NSDictionary based on the value - which in my case was neccessary - you can do the following:

    //sort typeDict alphanumeric to show it in order of values
        NSArray *keys = [typeDict allKeys];
    
        NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
            NSString *first = [typeDict objectForKey:a];
            NSString *second = [typeDict objectForKey:b];
            return [first compare:second];
        }];
        NSLog(@"sorted Array: %@", sortedKeys);
    
        NSMutableDictionary *sortedTypeDict = [NSMutableDictionary dictionary];
        int counter = 0;
        for(int i=0; i < [typeDict count]; i++){
            NSString *val = [typeDict objectForKey:[sortedKeys objectAtIndex:counter]];
            NSString *thekey = [sortedKeys objectAtIndex:counter];
            [sortedTypeDict setObject:val forKey:thekey];
            counter++;
        }
        NSLog(@"\n\nsorted dict: %@", sortedTypeDict);
    

    Not a big deal!

    0 讨论(0)
提交回复
热议问题