Re: Sorting an array
Great! Thanks a lot. (Just for the record: it's called sortUsingSelector: ) Best regards, Gabriel ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Sorting an array
> On Feb 11, 2020, at 4:59 AM, Gabriel Zachmann via Cocoa-dev > 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 (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Sorting an array
Gabriel, > On Feb 11, 2020, at 10:36, Gabriel Zachmann via Cocoa-dev > wrote: > > I have some trivial questions regarding sorting a simple array of strings. > > I used to use this code: > >NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey: nil > ascending: YES];//TODO: raus >[imagefiles_ sortUsingDescriptors: @[sd] ]; Personally I find it awkward to use a sort descriptor with an explicitly nil key. The API allows it, but it just feels off to me. Are you restricted to using an SDK that doesn’t have the -sortedArrayUsingComparator:^NSComparissonResult block parameter? If so, the selector based API is still pretty clean. Before the block version of the API we often wrote our own sort methods and used them as the selectors sorting. Way back, we implemented Cocoa NaturalLanguage sorting for a long time like this, by wrapping C API in Cocoa. I mention this just to illustrate that you can do a lot this way. But ultimately the sorting API you choose should depend on the content of the array. Sandor > > where imagefiles_ is an NSMutableArray* . > > Now, in the developer doc > ( > https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/2132-SW5) > > I saw a different method. Following that, I was thinking of sorting my array > like this: > >NSArray * sorted_images = [imagefiles_ sortedArrayUsingSelector: > @selector(localizedStandardCompare:) ]; >imagefiles_ = [NSMutableArray arrayWithArray: sorted_images]; > > First of all, what are the advantages/disadvantages of either method? > > Second, I was wondering if I could write the second method like this: > >imagefiles_ = [imagefiles_ sortedArrayUsingSelector: > @selector(localizedStandardCompare:) ]; > > > Thanks a lot in advance. > > > > ___ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > 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: > https://lists.apple.com/mailman/options/cocoa-dev/admin.szatmari.net%40gmail.com > > This email sent to admin.szatmari@gmail.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Sorting an array
I have some trivial questions regarding sorting a simple array of strings. I used to use this code: NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey: nil ascending: YES];//TODO: raus [imagefiles_ sortUsingDescriptors: @[sd] ]; where imagefiles_ is an NSMutableArray* . Now, in the developer doc ( https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#//apple_ref/doc/uid/2132-SW5) I saw a different method. Following that, I was thinking of sorting my array like this: NSArray * sorted_images = [imagefiles_ sortedArrayUsingSelector: @selector(localizedStandardCompare:) ]; imagefiles_ = [NSMutableArray arrayWithArray: sorted_images]; First of all, what are the advantages/disadvantages of either method? Second, I was wondering if I could write the second method like this: imagefiles_ = [imagefiles_ sortedArrayUsingSelector: @selector(localizedStandardCompare:) ]; Thanks a lot in advance. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com