I was going to use the following line of code:
[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES selector:@selector(caseInsensitiveCompare:)];
'caseInsensitiveCompare' is a method that I'm used to using on strings. But, the example shown that I was working from uses:
[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
(the difference being the word localized). What does this word do - how is the 'localized' method different to the normal method?
The Apple developer documentation is not very informative as to how these two methods differ.
This means that the comparator uses the national character sets when comparing.
In example polish language has letter Ł, which, in the national charaters set, is between L and M.
In example, when we have strings: Ltest, Łtest, Mtest, Ztest strings:
caseInsensitiveCompare gives in result: Ltest, Mtest, Ztest, Łtest
localizedCaseInsensitiveCompare gives in result: Ltest, Łtest, Mtest, Ztest
NSString provides both methods, caseInsensitiveCompare and localizedCaseInsensitiveCompare.
Certain locales may define different sorting criteria. If you are working with text localized for various locales, then use the localized version. Otherwise, use the standard version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With