• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Sorting NSArray -- advice on how to accomplish a "simple" alpha ordering?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


References: 
 >Re: Sorting NSArray -- advice on how to accomplish a "simple" alpha ordering? (From: Erik Stainsby <email@hidden>)

  • Prev by Date: Re: -[NSFileHandle readInBackgroundAndNotify] opens the file again
  • Next by Date: Re: Sorting NSArray -- advice on how to accomplish a "simple" alpha ordering?
  • Previous by thread: Re: Sorting NSArray -- advice on how to accomplish a "simple" alpha ordering?
  • Next by thread: Re: Sorting NSArray -- advice on how to accomplish a "simple" alpha ordering?
  • Index(es):
    • Date
    • Thread