I have an NSMutableDictionary with integer values, and I’d like to get an array of the keys, sorted ascending by their respective values. For example, with this dictionary:
mutableDict = {
"A" = 2,
"B" = 4,
"C" = 3,
"D" = 1,
}
I’d like to end up with the array ["D", "A", "C", "B"]. My real dictionary is much larger than just four items, of course.
The
NSDictionaryMethodkeysSortedByValueUsingComparator:should do the trick.You just need a method returning an
NSComparisonResultthat compares the object’s values.Your Dictionary is
And your Array is
Just use
NSNumberobjects instead of numeric constants.BTW, this is taken from:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Collections/Articles/Dictionaries.html