I am attempting to sort an NSDictionary.
From the Apple docs I see you can use keysSortedByValueUsingSelector
:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:63], @"Mathematics",
[NSNumber numberWithInt:72], @"English",
[NSNumber numberWithInt:55], @"History",
[NSNumber numberWithInt:49], @"Geography",
nil];
NSArray *sortedKeysArray = [dict keysSortedByValueUsingSelector:@selector(compare:)];
which gives:
// sortedKeysArray contains: Geography, History, Mathematics, English
but I want:
// sortedKeysArray contains: English, Mathematics, History, Geography
I have read that you can use compare:options:
, and an NSStringCompareOptions
to change the comparison to compare in the other direction.
However I don't understand how you send compare:options:
with an option in the selector.
I want to do something like this:
NSArray *sortedKeysArray = [dict keysSortedByValueUsingSelector:@selector(compare:options:NSOrderDescending)];
How should I switch the order of the comparison?
Related: https://discussions.apple.com/thread/3411089?start=0&tstart=0
Option 1: Use a comparator to invoke -compare:
in reverse: (Thanks Dan Shelly!)
NSArray *blockSortedKeys = [dict keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
// Switching the order of the operands reverses the sort direction
return [objc2 compare:obj1];
}];
Just reverse the descending and ascending return statements and you should get just what you want.
Option 2: Reverse the array you have:
See How can I reverse a NSArray in Objective-C?
I use :
NSSortDescriptor *sortOrder = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
self. sortedKeys = [[self.keyedInventoryItems allKeys] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortOrder]];
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