Documentation: sorting - compare: ... options and arguments
Ok, I obviously do not know how to locate specific items in the Apple Documentation.
I've already spent a couple of hours looking at class references with no success.
Can someone help me out.
All I want is to know how to do a simple REVERSE / descending sort comparison. (and how to look it up in the documentation)
Here is the line in my code to do a simple, Ascending sort:
NSArray * sortedKeysArray = [iams keysSortedByValueUsingSelector:@selector(compare:)];
How do I turn that into a reverse sort, or a descending sort?
All I can find is things like:
compare:
Returns an NSComparisonResult value that indicates whether the receiver is greater than, equal to, or less than a given number.
- (NSComparisonResult)compare:(NSNumber *)aNumber
Parameters
aNumber
The number with which to compare the receiver.
This value must not be nil. If the value is nil, the behavior is undefined and may change in future versions of Mac OS X.
Return Value
NSOrderedAscending if the value of aNumber is greater than the receiver’s, NSOrderedSame if they’re equal, and NSOrderedDescending if the value of aNumber is less than the receiver’s.
OR
SEL and @selector
Compiled selectors are assigned to a special type, SEL, to distinguish them from other data. Valid selectors are never 0. You must let the system assign SEL identifiers to methods; it’s futile to assign them arbitrarily.
The @selector() directive lets you refer to the compiled selector, rather than to the full method name. Here, the selector for setWidth:height: is assigned to the setWidthHeight variable:
OR
keysSortedByValueUsingComparator:
Returns an array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values using a given comparator block.
- (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr
Parameters
cmptr
A comparator block.
OR
keysSortedByValueUsingSelector:
Returns an array of the dictionary’s keys, in the order they would be in if the dictionary were sorted by its values.
- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator
Parameters
comparator
A selector that specifies the method to use to compare the values in the dictionary.
The comparator method should return NSOrderedAscending if the dictionary is smaller than the argument, NSOrderedDescending if the dictionary is larger than the argument, and NSOrderedSame if they are equal.
OR
NSComparisonResult
These constants are used to indicate how items in a request are ordered.
enum {
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
};
typedef NSInteger NSComparisonResult;
Somewhere I also saw an example where it gave me a block of code to do the simple comparisons to respond with NSOrderd(Same/Ascending/Descending), but I cannot believe that I have to write my own reverese sort algorithm. Right now, I cannot even find that documentation again.
And, yes, I cound just sort it normally, and get the count of array elements and then index from the end of the array.
But i think all that extra code would be ugly, and I'd like some simple CLEAN code.
I simply want to get an array, and examine the first 4 or 5 elements, sorted largest to smallest from the values in a dictionary,
(Sorry if I sound frustrated. I can't believe it is so difficult to find what I'm looking for.)
Any help or pointers will be appreciated.