Sorting ABPerson array with NSSortDescriptors
Sorting ABPerson array with NSSortDescriptors
- Subject: Sorting ABPerson array with NSSortDescriptors
- From: Gorazd Krosl <email@hidden>
- Date: Wed, 28 Jun 2006 13:40:54 -0400 (EDT)
>I'd like to sort an array of ABPersons using
"uniqueId" as a key (*)
>using sortedArrayUsingDescriptors, but it's not
working: the returned
>array has the same order as the input array. KVC
seems to be working,
>since I can take an ABPerson and get the ID with
valueForKey:@"uniqueId". Here's some simple test code:
> NSArray *p = [[ABAddressBook sharedAddressBook]
people];
> NSSortDescriptor *sd = [[[NSSortDescriptor alloc]
>initWithKey:@"uniqueId" ascending:YES] autorelease];
> NSArray *sda = [NSArray arrayWithObject:sd];
> NSArray *ps = [p sortedArrayUsingDescriptors:sda];
>Any ideas why it doesn't work?
>For now, I've rewritten my code to use
sortedArrayUsingSelector: with
>a category method for the comparison selector, but at
>some point I
>may put these ABPersons in a user-sortable table, so
>I'd like to
>figure out why NSSortDescriptors aren't working.
>Derrick
>(*) I lied. I'm really sorting by (last name, first
name) using
>methods in a category on ABPerson, but to avoid
confusing the issue,
>I tried with uniqueId and that doesn't work either.
My "lastName" and
>"firstName" methods never get called during the sort;
I presume the
>same is true of uniqueId.
Derrick,
It is not working because you are sorting array with
one object, which is itself an array:
NSArray *sda = [NSArray arrayWithObject:sd];
NSArray *ps = [p sortedArrayUsingDescriptors:sda];
Instead you should do:
NSArray *ps = [sd sortedArrayUsingDescriptors:sda];
HTH,
Gorazd
If all mankind minus one were of one opinion, and only one person were of the contrary opinion, mankind would be no more justified in silencing that one person, than he, if he had the power, would be justified in silencing mankind. On Liberty, John Stuart Mill (1806 - 1873)
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden