Search code examples
iosobjective-cios6nsdictionary

NSDictionary allKeys and then sorted with a selector vs keysSortedByValueUsingSelector


I am trying to get an array of all my keys in my NSDictionary into an array, and sorted using localizedCaseInsensitiveCompare. I first tried doing:

NSArray *test = [myDict keysSortedByValueUsingSelector@selector(localizedCaseInsensitiveCompare:)]]; 

I kept getting NSCFNumber localizedCaseInsensitiveCompare:]: unrecognized selector. I double checked and all of my 2 keys (for now) are strings.

I had to switch to doing to make it work:

NSArray *items = @[[[languages allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];

Why is that?!

Thanks!


Solution

  • Because is keysSortedBy**Value**UsingSelector and that's why you get NSNumber exception,because is sorting by value , not by key, you have NSNumbers as values.