Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between localizedCaseInsensitiveCompare: and caseInsensitiveCompare:?

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.

like image 896
Jordan Smith Avatar asked Nov 28 '22 02:11

Jordan Smith


2 Answers

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

like image 102
Tomasz Wojtkowiak Avatar answered Dec 09 '22 12:12

Tomasz Wojtkowiak


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.

like image 29
user371320 Avatar answered Dec 09 '22 12:12

user371320