Re: inconsistent behavior of NSString's localizedCaseInsensitiveCompare
Re: inconsistent behavior of NSString's localizedCaseInsensitiveCompare
- Subject: Re: inconsistent behavior of NSString's localizedCaseInsensitiveCompare
- From: Quincey Morris <email@hidden>
- Date: Sat, 05 May 2012 15:18:37 -0700
On May 5, 2012, at 14:07 , Martin Wierschin wrote:
> For example, consider the following sorted list of strings:
>
> "aaa" < "laso" < "lasso" < "zzz"
>
> I would expect that any new string I want to insert into this list would compare so that it has just one point of insertion. But this is not true if I test "laßt", where the German Eszett "ß" character sounds/behaves like "ss":
>
> (gdb) p (NSComparisonResult)[@"laßt" localizedCaseInsensitiveCompare:@"aaa"]
> $16 = 1
> (gdb) p (NSComparisonResult)[@"laßt" localizedCaseInsensitiveCompare:@"laso"]
> $17 = -1
> (gdb) p (NSComparisonResult)[@"laßt" localizedCaseInsensitiveCompare:@"lasso"]
> $18 = 1
> (gdb) p (NSComparisonResult)[@"laßt" localizedCaseInsensitiveCompare:@"zzz"]
> $19 = -1
>
> So, when using a binary search, I get different answers depending on the other strings in the list!
I think the answer lies in the current language in the locale.
"ß" within a string probably compares equal to "ss" at the corresponding position, independently of the language. (This makes sense, I think.) Therefore "laßt" > "lasso" always.
However, when the second word doesn't have "ss" in corresponding position, then the order is determined by pure character collating sequence for the language. In your case (which I'm guessing is English), 'ß' < 's'. In Markus's case (which I'm guessing is German), 'ß' > 's'.
(Or, it might be the other way around. The character collating sequence might depend on the locale rather than the language. In that case, I'd guess your locale was US even if your language was German, while Markus has German in the German locale.)
If I'm right, then sort order and search order are different, and you can't expect to use a binary search here. (Not unless you originally used pair-wise 'localizedCaseInsensitiveCompare' to manually sort the list of strings.)
_______________________________________________
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