Difference between Collator (locale-sensitive) and compareTo (lexicographically) for comparing String values
Asked Answered
C

2

2

I've been reading about using Collator and the compareTo method in String for comparing Strings. I'm unsure what the real difference is between the two from reading the API. When is one to prefer over the other?

API Collator

API String compareTo

Cellulitis answered 16/1, 2012 at 20:31 Comment(5)
Read the summary paragraphs -- first sentence really -- of both API. That should explain the basic difference. Perhaps the question can be refined from there?Marsha
Yes, but how does "lexicographically" differ from "locale-sensitive"? Why and when would I prefer one over the other?Cellulitis
Put that in the question/title :)Marsha
@Teletha: Use a collator: Suppose you have a contact manager for a company that has international locations. Suppose you have an autocomplete with prefix matching. The collator can allow your US employees to find a match on accented vowels in names without typing in the accents. Use compareTo: when you don't care about situations like I just gave.Unimposing
@Unimposing Awsome answer. Why just a comment? +1Cellulitis
U
3

Promoted from my comment (which sort of half answers the question):

Use a collator: Suppose you have a contact manager for a company that has international locations. Suppose you have an autocomplete with prefix matching. The collator can allow your US employees to find a match on accented vowels in names without typing in the accents.

Use compareTo: when you don't care about situations like I just gave.

Unimposing answered 16/1, 2012 at 23:53 Comment(0)
O
7

Basically, locale-sensitive means that it takes into account the language being used and may use different weights for comparisons between different characters.

"For example, in Czech, "e" and "f" are considered primary differences, while "e" and "ě" are secondary differences, "e" and "E" are tertiary differences and "e" and "e" are identical." 1

With the lexicographical comparison of compareTo it just uses their Unicode values instead of taking these different weights into account.

"For comparing Strings exactly once, the compare method provides the best performance. When sorting a list of Strings however, it is generally necessary to compare each String multiple times. In this case, CollationKeys provide better performance. The CollationKey class converts a String to a series of bits that can be compared bitwise against other CollationKeys. A CollationKey is created by a Collator object for a given String. "1

1 Colator Javadoc

Operand answered 16/1, 2012 at 21:55 Comment(0)
U
3

Promoted from my comment (which sort of half answers the question):

Use a collator: Suppose you have a contact manager for a company that has international locations. Suppose you have an autocomplete with prefix matching. The collator can allow your US employees to find a match on accented vowels in names without typing in the accents.

Use compareTo: when you don't care about situations like I just gave.

Unimposing answered 16/1, 2012 at 23:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.