Quick revision
The short answer
Comparator.comparing(Item::name) uses String.compareTo, which compares UTF-16 code units, so uppercase sorts before lowercase and accented letters land after z, which is wrong for people in almost every language. A multi-tenant search API needs a java.text.Collator for each tenant's locale with the right strength, per-thread or cloned Collator instances because they are not guaranteed thread-safe, precomputed CollationKey values for large sorts, and a unique tie-breaker such as the ID so the order is stable and pagination is consistent.
What to remember
In this answer, we saw that Comparator.comparing(Item::name) sorts by UTF-16 code units, which puts uppercase before lowercase and accented letters after z. A multi-tenant API needs a Collator per tenant locale with a chosen strength and decomposition, cloned or per-thread instances because Collator is not guaranteed thread-safe, CollationKey values to make large sorts affordable, and a unique tie-breaker such as the ID so that the order, and therefore pagination, is stable and deterministic.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Comparator.comparing(Item::name) uses String.compareTo, which compares UTF-16 code units, so uppercase sorts before lowercase and accented letters land after z, which is wrong for people in almost every language. A multi-tenant search API needs a java.text.Collator for each tenant's locale with the right strength, per-thread or cloned Collator instances because they are not guaranteed thread-safe, precomputed CollationKey values for large sorts, and a unique tie-breaker such as the ID so the order is stable and pagination is consistent.
Why this question is asked
Sorting by name looks like the simplest feature in a search API, and it is the one users notice first when it is wrong: a German tenant sees names with umlauts at the end of the list, a Swedish tenant sees them in the wrong place for Swedish, and the same item shows up on two pages. Interviewers ask this design question to see whether we know that string order is a linguistic question, not a byte question, and whether we can make it correct, fast and deterministic at once. In this answer, we will see why String.compareTo is wrong for humans, how Collator fixes it per locale, how to handle its thread-safety and cost, and how to make the order stable for pagination.
1Why String.compareTo Is Wrong for Human Sorting
String.compareTo compares strings character by character using the numeric values of their UTF-16 code units, the 16-bit units Java uses to store text. That order is fast and well defined, but it is the order of the Unicode tables, not of any language. All uppercase Latin letters (65 to 90) come before all lowercase ones (97 to 122), and letters with accents, such as e with an acute accent (233), come after z.
A person would expect apple and Apple next to each other near the start, and the word starting with an accented e next to other words starting with e. Different languages also disagree with each other: in German, a with an umlaut sorts with a, while in Swedish it is a separate letter that sorts after z. So there is no single correct order; there is a correct order for each locale, which in a multi-tenant system means for each tenant.
2Using a Collator for the Tenant's Locale
java.text.Collator implements locale-sensitive string comparison. We obtain one for the tenant's locale and use it as the comparator for the name key. Its strength decides which differences count: PRIMARY compares base letters only, ignoring case and accents; SECONDARY also distinguishes accents; TERTIARY, the default, also distinguishes case.
| Strength | Treats as equal | Typical use |
|---|---|---|
| PRIMARY | a, A, a with accent | Loose search-like ordering |
| SECONDARY | a and A, but not a with accent | Most display sorting |
| TERTIARY (default) | only identical letters | Strict, case-sensitive order |
| IDENTICAL | only identical strings | Rarely needed |
Notice the decomposition setting. An accented letter can be stored as one code point or as a base letter followed by a combining accent; both look identical on screen. Canonical decomposition makes the collator treat them as the same, which matters when names come from different systems. The strength is a product decision per field: for a list of product names, ignoring case is usually right; for a list of codes, it may not be.
3Collator Is Not Thread-Safe: Clone It or Keep One Per Thread
Collator instances are not guaranteed to be thread-safe, and in a web service many request threads sort at the same time. Sharing one mutable Collator between them is a latent bug. We keep a prototype per tenant locale, configured once, and give each use its own copy, either by cloning or with a ThreadLocal.
Cloning is cheap compared with creating a collator from its rules, and a clone per request avoids thread-safety questions entirely. This is one of the few places where clone() on a JDK class is the documented, intended approach.
4Large Sorts: Precompute CollationKey Values
Collator.compare is much more expensive than String.compareTo, because it has to apply the language's rules each time. A sort makes about n log n comparisons, so for 100,000 items that is around 1.7 million collator comparisons, each re-deriving the same information for the same strings. A CollationKey is a precomputed binary form of a string under a collator's rules, and comparing two keys is a fast bitwise comparison.
So we pay the expensive work once per item instead of once per comparison. For a sort of a few dozen items, a plain collator comparator is fine; the keys pay off when sorts are large or frequent. If the API sorts very large result sets, the better answer is usually to let the search engine or the database sort, configured with the same linguistic rules, which the long-term design must keep in step.
5Stable, Deterministic Order for Pagination
A search API returns results a page at a time. If two items compare as equal, which is common at SECONDARY strength (apple and Apple), their relative order may differ between requests, and an item can appear on both page 1 and page 2 while another is never shown. So the sort definition always ends with a unique tie-breaker, usually the item's ID, as in the code above.
The same definition must be used everywhere the order is produced: the comparator in Java, the sort in the search engine and the cursor that remembers where a page ended, which should contain the sort key and the ID of the last item, not a page number. Then every request continues exactly after the last item the user saw.
6Follow-up questions
Is java.text.Collator enough, or do we need ICU4J?
java.text.Collator covers the main languages and is enough for many systems. ICU4J offers more complete and more current Unicode collation data, options such as numeric ordering (item 2 before item 10) and closer agreement with databases and search engines that use ICU. If tenants need those features, ICU4J is worth the dependency; check what your JDK version supports first.
Should the tenant choose the locale, or the user?
Usually the tenant sets a default and the user may override it, so the API should accept the locale as an explicit parameter with the tenant default as a fallback. Caching and cursors must then include the locale, because the same cursor means a different position under a different collation.
How does this interact with search relevance sorting?
Relevance is a score, not a string, so collation does not apply to it, but ties in score are common. The same rule applies: break ties with a deterministic key, such as the collated name and then the ID, so pages do not overlap.
7Conclusion
In this answer, we saw that Comparator.comparing(Item::name) sorts by UTF-16 code units, which puts uppercase before lowercase and accented letters after z. A multi-tenant API needs a Collator per tenant locale with a chosen strength and decomposition, cloned or per-thread instances because Collator is not guaranteed thread-safe, CollationKey values to make large sorts affordable, and a unique tie-breaker such as the ID so that the order, and therefore pagination, is stable and deterministic.
Open with
The first problem is that comparing by name uses String.compareTo, which compares UTF-16 code units. That puts all uppercase letters before lowercase and accented letters after z, and it ignores the fact that languages disagree, for example German and Swedish sort letters with umlauts differently.
Then, if they want more
So I would sort names with a java.text.Collator for the tenant's locale, with the strength chosen per field, usually SECONDARY to ignore case but keep accents, and canonical decomposition so composed and decomposed accents compare the same.
Collator instances are not guaranteed to be thread-safe, so I would keep a configured prototype per locale and clone it per request, or use a ThreadLocal. For large sorts, collator comparisons are expensive, so I would precompute CollationKey values once per item and sort by the keys, or push the sort to the search engine with matching collation.
Finally, I would always end the sort definition with a unique tie-breaker, the item ID, because at SECONDARY strength apple and Apple compare equal, and without a tie-breaker their order can change between requests and items get duplicated or skipped across pages. The cursor should carry the sort key and ID of the last item, plus the locale.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You are designing sort and ranking rules for a multi-tenant search API with...": https://scrivio-interview-library.netlify.app/java/object-contracts/dde98f03ca/