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: "Gerriet M. Denkmann" <email@hidden>
- Date: Wed, 01 Aug 2012 21:55:55 +0700
On 1 Aug 2012, at 21:38, Michael Babin <email@hidden> wrote:
>
> On Aug 1, 2012, at 8:00 AM, Erik Stainsby <email@hidden> wrote:
>
>> So I have a dictionary like so:
>>
>> NSDictionary * countries = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Australia",@"Canada",@"United Kingdom",@"United States",nil] forKeys:[NSArray arrayWithObjects:@"au",@"ca",@"uk",@"us",nil]];
>>
>> I want to present them alphabetically as menuItems in an NSPopUpButton. I grab the list of keys:
>>
>> NSMutableArray * sortkeys = [NSMutableArray arrayWithArray:[countries allKeys]];
>>
>> What I have come up with is this:
>>
>> [sortkeys sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
>> return [(NSString*)obj1 compare:(NSString*)obj2 ];
>> } ];
>>
>> Is this a sane approach ? Seems a bit fussy to have to spec the cast like this. Or is this just the what-is of this kind of functionality?
>
> If your comparison is only going to be a single method call with a single parameter like this, then probably a bit simpler to use:
>
> [sortkeys sortUsingSelector:@selector(compare:)];
The documentation says: "If you are comparing strings to present to the end-user, you should typically use localizedCompare: or localizedCaseInsensitiveCompare: instead."
Kind regards,
Gerriet.
_______________________________________________
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