Re: -[NSString localizedCompare:] - example of semantic difference with compare?
Re: -[NSString localizedCompare:] - example of semantic difference with compare?
- Subject: Re: -[NSString localizedCompare:] - example of semantic difference with compare?
- From: Ricky Sharp <email@hidden>
- Date: Mon, 27 Mar 2006 15:51:36 -0600
On Mar 24, 2006, at 2:29 PM, Deborah Goldsmith wrote:
Hmmm. There doesn't appear to be a way to do this with the NSString
API. You can do it with CFStringCompareWithOptions:
enum CFStringCompareFlags {
kCFCompareCaseInsensitive = 1,
kCFCompareBackwards = 4,
kCFCompareAnchored = 8,
kCFCompareNonliteral = 16,
kCFCompareLocalized = 32,
kCFCompareNumerically = 64
};
Of course, you can pass an NSString to CFStringCompareWithOptions.
This works well, thanks!
The new code (which lives as a category on NSString):
CFComparisonResult theCFResult = CFStringCompareWithOptions
((CFStringRef) self,
(CFStringRef) anItemName, CFRangeMake (0, CFStringGetLength
((CFStringRef) self)),
kCFCompareCaseInsensitive | kCFCompareLocalized |
kCFCompareNumerically);
Now I get beautiful output for my English and Hawaiian test runs:
English
-------
A hui hou
'Ae
'A'ole
Maika'i no au
student
Student #1
Student #2
Student #10
Student #11
<chinese>
Hawaiian
--------
A hui hou
Maika'i no au
'Ae
'A'ole
student
Student #1
Student #2
Student #10
Student #11
<chinese>
Once again, (') is actually U+02BB and <chinese> is U+7389
You might want to file a bug against NSString to add either
localizedCompare:withOptions:, or to add a NSLocalizedSearch option.
Will definitely do this tonight; I'll actually request both options
just for completeness sake.
From my original e-mail when using localizedCompare: ...
On Mar 23, 2006, at 6:30 PM, Ricky Sharp wrote:
English
-------
Student #1
Student #2
Student #10
student #11
Hawaiian
--------
Student #1
Student #2
Student #10
student #11
This was a bad cut-and-paste. In both cases here, the output was this:
Student #1
Student #10
student #11
Student #2
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden