• 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 Array Alphabetically
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sorting Array Alphabetically


  • Subject: Re: Sorting Array Alphabetically
  • From: Ricky Sharp <email@hidden>
  • Date: Fri, 13 Apr 2007 18:11:07 -0500


On Apr 13, 2007, at 3:17 PM, Deborah Goldsmith wrote:

I think the first one will work the way you want by passing NSString's compare:

Not quite. Based on the sample data, I'm guessing this sort is intended to be shown to an end user. compare: should *never* be used to sort a list for display to an end user. It does a comparison based on the Unicode code point values, which is not what most users would consider "sorted."


Lists being sorted for end users should *always* use localizedCompare: or localizedCaseInsensitiveCompare:. Only use compare: if the end user will not see the ordering.

Actually, I was told to use (which I do) compare:options:range:locale: for user-displayed items. This tends to mimic as close as possible what Finder will do.


For example:

@implementation NSString (SortingExample)

- (NSComparisonResult)compare1:(NSString*)aString
{
    return [self localizedCompare:aString];
}

- (NSComparisonResult)compare2:(NSString*)aString
{
NSDictionary* theLocaleDictionary = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];


return [self compare:aString options:NSCaseInsensitiveSearch | NSNumericSearch
range:NSMakeRange (0, [self length]) locale:theLocaleDictionary];
}


@end

@implementation MyController

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
NSMutableArray* theFirstArray =
[NSMutableArray arrayWithObjects:@"student #3", @"abc", @"student #10", @"student #1",
@"def", @"pqr", nil];
NSMutableArray* theSecondArray = [theFirstArray mutableCopy];


[theFirstArray sortUsingSelector:@selector(compare1:)]; // localizedCompare:
[theSecondArray sortUsingSelector:@selector(compare2:)]; // compare:options:range:locale:

NSLog (@"localizedCompare: = %@", theFirstArray);
NSLog (@"compare:options:range:locale: = %@", theSecondArray);
}


@end

Output is...

localizedCompare: = (abc, def, pqr, "student #1", "student #10", "student #3")
compare:options:range:locale: = (abc, def, pqr, "student #1", "student #3", "student #10")



Note that I used ASCII7 values for the sake of this e-mail. Both will work with Unicode and will also honor the user's localization prefs. The main reason to not use localizedCompare, as you can see, is when dealing with ordinal values in the strings.


___________________________________________________________
Ricky A. Sharp         mailto:email@hidden
Instant Interactive(tm)   http://www.instantinteractive.com

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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: Sorting Array Alphabetically
      • From: Deborah Goldsmith <email@hidden>
References: 
 >Sorting Array Alphabetically (From: Micha Fuhrmann <email@hidden>)
 >Re: Sorting Array Alphabetically (From: Shamyl Zakariya <email@hidden>)
 >Re: Sorting Array Alphabetically (From: Deborah Goldsmith <email@hidden>)

  • Prev by Date: Bindings, and Core Image
  • Next by Date: Re: Bindings, and Core Image
  • Previous by thread: Re: Sorting Array Alphabetically
  • Next by thread: Re: Sorting Array Alphabetically
  • Index(es):
    • Date
    • Thread