Search code examples
javacompareto

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


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


Solution

  • 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.