Objective-C: Sort keys of NSDictionary based on dictionary entries
cocoa-touch, objective-c, xcode
Solution
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];
}];
Problem
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.