Home:ALL Converter>sorting dictionary by child key

sorting dictionary by child key

Ask Time:2011-06-25T04:26:12         Author:user7865437

Json Formatter

I understand how to display a dictionaries sorted content by means of obtaining allKeys into an NSMutableArray, sorting the array and using it as an index into the dictionary, but....

What if I want to sort the parent keys by one of their child keys?

for example, a dictionary with parent keys @"8979", @"6754", @8776", @"9076" and so on have child key / value pairs like:

@"pos", 8 @"pos", 4 @"pos", 10 @"pos", 7

and so on.

I know how to sort the parent keys to be: @"6754", @"8776", @"8979", @"9076"

but how to sort the parent keys based on their respective @"pos" child key resulting in: @"6754", @"9076", @"8979", @"8776"

???

Author:user7865437,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/6473390/sorting-dictionary-by-child-key
stevex :

You can simplify this to a single line:\n\nNSArray *sortedArray = [keySort keysSortedByValueUsingComparator: ^(id obj1, id obj2) {\n return [(NSString *)obj1 compare:(NSString *)obj2];\n}];\n",
2011-12-24T04:22:28
highlycaffeinated :

keysSortedByValueUsingSelector: will let you supply a comparator that is passed the values from your dictionary and returns an ordered array of the keys.",
2011-06-24T20:28:56
yy