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 the method that I use for use in strings. But, an example showed that I worked from using:

 [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; 

(difference is a localized word). What does this word do - how does a "localized" method differ from a regular method?

Apple's developer documentation is not very informative about how the two methods differ.

+4
source share
4 answers

NSString provides both methods: caseInsensitiveCompare and localizedCaseInsensitiveCompare.

Certain locales can define different sorting criteria. If you are working with text localized for different locales, use the localized version. Otherwise, use the standard version.

+10
source

This means that the comparator uses national character sets when comparing.
In the example in Polish, there is the letter Ł, which in the national character set is between L and M.

In the example, when we have the lines: Ltest, Łtest, Mtest, Ztest string:

caseInsensitiveCompare yields: Ltest, Mtest, Ztest, Łtest
localizedCaseInsensitiveCompare results in: Ltest, Łtest, Mtest, Ztest

+13
source

localizedCaseInsensitiveCompare: - a localized version of caseInsensitiveCompare: Since it does not really matter in English, it is, for example, in Polish for ł, ą, ę , etc. or in Spanish for ñ, é, á , etc. or French for ç, è, ê , etc. They have a different position in the alphabet depending on the locale.

+5
source

The localizedCaseInsensitiveCompare: method will use any rules that exist for the current locale ( [NSLocale currentLocale] ) when sorting your data. These rules often include numbering precedence, non-ASCII character order, etc.

Basically, if you do not expect character-based ordering, you should use a localized method.

If you haven’t worked with localization before, I would recommend checking the following links:

+5
source

Source: https://habr.com/ru/post/1389886/


All Articles