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

Re: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects


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

On Jun 6, 2008, at 2:26 PM, Andrew Merenbach wrote:

Quoting Ken Thomases <email@hidden>:

On Jun 6, 2008, at 3:43 AM, George Stuart wrote:

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.

Hmm. Good question. You could use @"description" as the key to eliminate the uncertainty. Or maybe @"self".

Cheers,
Ken


Hi,

This may not be an option in every case, but would making a "wrapper" class for your strings work? It'd just be a basic class with an accessor/property and a corresponding ivar that would then be the sort key.

Cheers,
Andrew


Thanks very much for the responses! I'm leaning away from writing a wrapper as I'd rather try and leverage the base objects as much as possible. Adding a wrapper will add complexity -- every place a string is inserted needs to be sure to first wrap the NSString in my custom wrapper. It's a minor change, but would like to avoid this if possible.

The suggestion of using a keyPath of @"self" presents another question:
1) I assume NSSortDescriptor is using valueForKeyPath:, which in turns calls valueForKey: some number of times, to get the object for the keyPath I specified. Is it defined behavior that sending an object valueForKey:@"self" will return the object itself? I guess this is ultimately the same as my original question, "is it defined behavior that sending an object valueForKey:nil will return the object itself?"


As for using a keyPath of @"description", I think this might be the best way for NSString. I completely forgot about this option. The docs say:

	Returns the receiver.

	- (NSString *)description

Duh... That's precisely what I need :).

Thanks,
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: "Adam R. Maxwell" <email@hidden>
References: 
 >Using NSSortDescriptor to reverse sort an NSArray containing NSString objects (From: George Stuart <email@hidden>)
 >Re: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects (From: Ken Thomases <email@hidden>)
 >Re: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects (From: Andrew Merenbach <email@hidden>)

  • Prev by Date: Re: OK on Enter, not Return
  • Next by Date: Re: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects
  • Previous by thread: Re: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects
  • Next by thread: Re: Using NSSortDescriptor to reverse sort an NSArray containing NSString objects
  • Index(es):
    • Date
    • Thread