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: Jens Alfke <email@hidden>
- Date: Wed, 01 Aug 2012 09:41:29 -0700
On Aug 1, 2012, at 6:00 AM, Erik Stainsby <email@hidden> wrote:
> 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?
The "(NSString*)" casts are unnecessary, as "id" is type-compatible with any object-pointer type. You can also remove the "NSComparisonResult", as the compiler can infer the return type from the 'return' statement.
Alternatively you can change the parameter types to NSString*, which makes the block more type-safe:
[sortkeys sortUsingComparator:^(NSString* obj1, NSString* obj2) {
return [obj1 compare:obj2 ];
} ];
Or since your block just calls one method, you could just use:
[sortKeys sortUsingSelector: @selector(compare:)];
—Jens
_______________________________________________
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