On 01/08/2012, at 3:07 PM, Graham Cox wrote:

> NSSortDescriptor* desc = [NSSortDescriptor sortDescriptorWithKey:@"compare:" 
> ascending:YES];


Oops, this wasn't what I meant.

The "key" should be the property you're comparing to sort the objects. If your 
array is a bunch of strings to be sorted, then what is the key? Well, it's 
actually "self":

NSSortDescriptor* desc = [NSSortDescriptor sortDescriptorWithKey:@"self" 
ascending:YES];

Because internally the descriptor is looping over the array and doing a 
-valueForKey:@"self" on each object, and that returns the object itself. The 
sort comparison itself is done by invoking -compare: on the objects.


If it's another sort of object that has a string property, then you would pass 
the name of that property, e.g. @"lastName".

--Graham


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to