Re: Sorting NSArray -- advice on how to accomplish a "simple" alpha ordering?
Re: Sorting NSArray -- advice on how to accomplish a "simple" alpha ordering?
- Subject: Re: Sorting NSArray -- advice on how to accomplish a "simple" alpha ordering?
- From: Graham Cox <email@hidden>
- Date: Wed, 01 Aug 2012 15:13:50 +1000
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 (email@hidden)
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:
This email sent to email@hidden