JoshDG
JoshDG

Reputation: 3931

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

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 corresponding values in the dictionary (alphabetically). So if the dictionary contains key=someString, then I want to sort keys based on the strings they correspond to. I think its some application of sortUsingComparator but its a little out of my reach at this point.

Upvotes: 13

Views: 23450

Answers (5)

dilip
dilip

Reputation: 163

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);

Upvotes: 1

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]];

Upvotes: 3

Torsten Barthel
Torsten Barthel

Reputation: 3486

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!

Upvotes: 1

mask8
mask8

Reputation: 3638

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];
}];

Upvotes: 21

user529758
user529758

Reputation:

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.

Upvotes: 3

Related Questions