Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

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.

Posted on Oct 19, 2011 2:57 AM

Reply
1 reply

Oct 19, 2011 3:57 AM in response to reststop

Ok, I found the Block method, example I mentioned:


Listing 6 Blocks ease custom sorting of dictionaries


NSArray *blockSortedKeys = [dict keysSortedByValueUsingComparator: ^(id obj1, id obj2) {

if ([obj1 integerValue] > [obj2 integerValue]) {

return (NSComparisonResult)NSOrderedDescending;

}

if ([obj1 integerValue] < [obj2 integerValue]) {

return (NSComparisonResult)NSOrderedAscending;

}

return (NSComparisonResult)NSOrderedSame;

}];


but that seems like a lot when all I want is to let the selector compare: do it's thing, in descending order.So I could do the following, just reversing the greater-than and less-than comparisons...

NSArray *blockSortedKeys = [dict keysSortedByValueUsingComparator: ^(id obj1, id obj2) {

if ([obj1 integerValue] < [obj2 integerValue]) {

return (NSComparisonResult)NSOrderedDescending;

}

if ([obj1 integerValue] > [obj2 integerValue]) {

return (NSComparisonResult)NSOrderedAscending;

}

return (NSComparisonResult)NSOrderedSame;

}];


Still, I'd like to find a complete description, with examples of using the compare: selector, and how to provide options or ordering in a simple manner without having to re-code things that (I have to believe) already exist.


Thanks in advance for pointers to the documentation.....


Or am I just going about this sorting thing the wrong way?

Documentation: sorting - compare: ... options and arguments

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.