Re: Sorting an array
Re: Sorting an array
- Subject: Re: Sorting an array
- From: Jens Alfke via Cocoa-dev <email@hidden>
- Date: Tue, 11 Feb 2020 10:27:46 -0800
> On Feb 11, 2020, at 4:59 AM, Gabriel Zachmann via Cocoa-dev
> <email@hidden> wrote:
>
> First of all, what are the advantages/disadvantages of either method?
Sort descriptors are data; selectors are [references to] code. So a sort
descriptor lets you configure the sorting without having to write any custom
code, and there's a standard API for it. For this reason they're used by
NSTableView to represent the column sorting, and by NSArrayController.
If you're hardcoding the sort order, which it looks like you are, then it's
probably simpler just to go the code route and sort using a selector. (Note
that there are also sort methods that take an NSComparator, which is just a
typedef for a block. This is very convenient for custom sorts.
> Second, I was wondering if I could write the second method like this:
>
> imagefiles_ = [imagefiles_ sortedArrayUsingSelector:
> @selector(localizedStandardCompare:) ];
That won't work since imageFiles_ is a mutable array, and
-sortedArrayUsingSelector: returns an immutable array. Use the mutable version,
-sortArrayUsingSelector:, instead.
—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: | |
| >Sorting an array (From: Gabriel Zachmann via Cocoa-dev <email@hidden>) |