I have a dictionary whose keys consist of NSNumber
s. I am using keysSortedByValueUsingComparator
as follows:
NSArray *sortedKeys = [self.platformDict keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [(NSNumber*)obj2 compare:(NSNumber*)obj1];
}];
However here is the result I get:
(lldb) po sortedKeys
(NSArray *) $1 = 0x0704bd20 <__NSArrayI 0x704bd20>(
100000,
250000,
1000000,
500000,
3000000,
2000000,
5000000,
10000000
)
Which is out of order. Is this a bug with the method implementation or is there another issue here?
Perhaps you're misunderstanding what the keysSortedByValue:...
method does. It does not sort the keys (for that you would just sort the array returned by allKeys
), instead it sorts the values and then applies their order to the keys.
So let's say you have the following dictionary:
{"Orange": 1, "Apple": 3, "Peach": 2}
The result would be:
"Orange", "Peach", "Apple"
because that corresponds to the sorted order of the values (1, 2, 3).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With