Re: inconsistent behavior of NSString's localizedCaseInsensitiveCompare
Re: inconsistent behavior of NSString's localizedCaseInsensitiveCompare
- Subject: Re: inconsistent behavior of NSString's localizedCaseInsensitiveCompare
- From: Martin Wierschin <email@hidden>
- Date: Mon, 07 May 2012 13:35:01 -0700
>>> 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.)
>>
>> All comparisons, including those used for the original sorting, were done using the same comparison method/locale. And the sort order and search order need to be same, otherwise any algorithms that rely on the transitive nature of comparisons (eg: quicksort, binary search, etc) will fail.
>
> If I understand Quincey correctly, that's exactly what he's saying: the semantics of localizedCaseInsensitiveCompare: might be such that it is not appropriate for such algorithms.
As Jens mentioned, that doesn't make any sense. What good is a localized comparison method if not for sorting a list for display? I suppose you could be implying that Cocoa's sorting methods aren't optimized to assume comparisons are transitive (or special cases problematic strings/comparisons), but that seems quite unlikely.
Actually, it's possible to test indirectly. I bet we could construct a set of strings that sorts differently depending on the input order. Ah yes:
- (void) testSorting
{
NSArray* words = [NSArray arrayWithObjects:@"laso", @"lassos", @"las", @"moo", @"lasso", @"aaa", @"lassoMore", @"le", @"mum", nil];
NSString* problemWord = @"laßt";
SEL sortSelector = @selector(localizedCaseInsensitiveCompare:);
NSArray* sorted = [[words arrayByAddingObject:problemWord] sortedArrayUsingSelector:sortSelector];
// words should sort consistently, no matter where the problem word is inserted
NSUInteger count = [words count];
for( NSUInteger insertIndex = 0; insertIndex < count; insertIndex++ ) {
NSMutableArray* testSort = [[words mutableCopy] autorelease];
[testSort insertObject:problemWord atIndex:insertIndex];
[testSort sortUsingSelector:sortSelector];
STAssertTrue( [testSort isEqual:sorted], @"sorted array should always be the same" );
}
}
That fails 5 of out the 9 loop iterations.
Well, I'm certainly very happy that Apple implements all the Unicode collation rules for us for various locales! But I think this must be classified as a bug.
~Martin
_______________________________________________
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