• 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
Using NSSortDescriptor to reverse sort an NSArray containing NSString objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using NSSortDescriptor to reverse sort an NSArray containing NSString objects


  • Subject: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects
  • From: George Stuart <email@hidden>
  • Date: Fri, 6 Jun 2008 03:43:43 -0500

So I searched through the cocoabuilder.com threads on array sorting to see what had been discussed regarding this topic. My goal is to reverse alphabetically sort an array of NSString objects. I saw a couple suggestions:

1. Categories to add funcationality to NSString and then use the NSArray method sortedArrayUsingSelector(reverseLocalizedCompare:), where reverseLocalizedCompare: is the new method I added.

2. Implement a straight C function and use sortedArrayUsingFunction:context:hint: to call it.

I would rather use pure Cocoa for this and came up with the following:

NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:nil ascending:NO selector:@selector(localizedCompare:)];
NSArray *sortedArray = [unsortedArray sortedArrayUsingDescriptors: [NSArray arrayWithObject:desc]];
[desc release];


That seems to work just fine... but I'm worried. I "guessed" at using the key of "nil" since I want the actual objects in the array to be the target for the @selector. Is it a fluke that this works? I don't want to rely on undefined behavior.

Thanks in advance,
George

_______________________________________________

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


  • Follow-Ups:
    • Re: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects
      • From: Ken Thomases <email@hidden>
  • Prev by Date: Re: Regular Expressions?
  • Next by Date: Re: Regular Expressions?
  • Previous by thread: Re: List Guidelines and Resources
  • Next by thread: Re: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects
  • Index(es):
    • Date
    • Thread