JavaScript's default string order is not a reliable multilingual alphabet. This guide shows how to use Intl.Collator and String.prototype.localeCompare() to sort, compare, and filter Unicode text, with a reusable checklist for names, accented words, numbers, Turkish, German, Swedish, Arabic, and Japanese data.
Overview
Comparing strings with a < b or Array.prototype.sort() compares UTF-16 code units. That is useful for technical identifiers, but it does not represent the alphabetical rules people expect in different languages. Accented letters, case, digits, kana, and script-specific ordering can all produce surprising results.
Intl.Collator provides locale-aware comparison. Create a collator for the language and behavior your interface needs, then pass its comparison function to sort() or call it directly for equality and ordering checks.
const collator = new Intl.Collator("de", {
sensitivity: "base",
numeric: true
});
const names = ["Änne", "Anna", "Özil", "Zara"];
names.sort(collator.compare);
const sameForSearch = collator.compare("café", "CAFE") === 0;
The comparison result is negative, zero, or positive. Do not depend on a particular number such as -1 or 1; only its sign is part of the useful contract.
localeCompare() or Intl.Collator?
Both use the same internationalization machinery in modern JavaScript environments, but they suit different workflows.
| Need | localeCompare() | Intl.Collator |
|---|---|---|
| One comparison | Concise and convenient | More setup than necessary |
| Sorting many values | Works, but options are repeated if called inline | Create once and reuse collator.compare |
| Shared search and sort rules | Can express the rules per call | Centralizes locale and options |
| Options | Pass locale and options to each call | Configure once at construction |
| Inspecting behavior | Limited to the comparison call | Use resolvedOptions() and supportedLocalesOf() |
For example, a one-off comparison can be written as "ångström".localeCompare("zebra", "sv"). For a list, prefer const collator = new Intl.Collator("sv"); items.sort(collator.compare);. Reusing one configured collator also makes it less likely that sorting and filtering silently use different rules.
Important options
| Option | Useful values | What it controls |
|---|---|---|
usage | sort, search | Whether the comparison is intended for ordering or matching. Use the value that describes the interface behavior. |
sensitivity | base, accent, case, variant | How differences in base letters, accents, and case affect comparison. |
numeric | true, false | Whether digit sequences are compared numerically, so values such as item2 can come before item10. |
caseFirst | upper, lower, false | Whether uppercase or lowercase should be preferred when case is significant. The locale can influence the default. |
ignorePunctuation | true, false | Whether punctuation should have less influence on comparison. Validate this carefully for identifiers and codes. |
Option support and defaults are locale-sensitive. Treat the requested configuration as a rule to test, not as a guarantee that every language orders every character in the same way.
Checklist by scenario
1. Sorting names and accented words
- Choose the locale from the user's language or the data's established editorial convention.
- Decide whether accents and case should distinguish entries.
- Create one collator and use it for the complete sort.
- Test names that differ only by case, accent, or a language-specific letter.
const nameCollator = new Intl.Collator("fr", {
usage: "sort",
sensitivity: "base"
});
const people = ["Émile", "emile", "Zoë", "Zoe"];
people.sort(nameCollator.compare);
sensitivity: "base" is often appropriate for a forgiving directory, but it can make distinct records compare as equal. Do not use a zero comparison as a database identity test unless that equivalence is explicitly intended.
2. Sorting values with numbers
Use numeric: true when digit sequences represent quantities or versions in a human-facing list.
const fileCollator = new Intl.Collator("en", { numeric: true });
["chapter10", "chapter2", "chapter1"].sort(fileCollator.compare);
Test leading zeros, decimal-looking strings, negative signs, and mixed letters before applying this to filenames or product codes. If the values are machine identifiers with a strict format, an explicit parser may be more predictable than locale-aware comparison.
3. Filtering and search
For exact or whole-value matching, compare with a collator rather than lowercasing both strings. This avoids assuming that lowercase conversion is a complete model of language-aware equality.
const searchCollator = new Intl.Collator("tr", {
usage: "search",
sensitivity: "base"
});
const matches = searchCollator.compare("İstanbul", "istanbul") === 0;
Substring search is a separate problem. Intl.Collator compares complete strings; it does not return a match index or automatically find a locale-aware substring inside a longer string. For an autocomplete interface, combine a deliberate search strategy with tested normalization and locale rules, then use the collator for candidate comparison where appropriate.
4. Testing language-specific behavior
- Turkish: Include dotted and dotless I, such as
İ,i,I, andı. Test withtrrather than assuming English case behavior. - German: Test
ä,ö,ü, andßagainst the ordering expected by your product or editorial rules. German collation conventions can differ from a simple character-code order. - Swedish: Include
å,ä, andö. Do not infer Swedish ordering from English or German results; test withsv. - Arabic: Use real Arabic words and review the displayed order in an RTL interface. Sorting order and visual direction are related to the product experience but are not the same operation.
- Japanese: Test kanji, hiragana, katakana, and names with the locale and collation behavior your application expects. A displayed reading may be required when users need a specific gojūon-style directory order.
What to double-check
- Locale selection: Confirm whether the locale comes from the user, tenant, document, or a fixed business rule. A browser's default locale may not match the language of the dataset.
- Fallback behavior: Check which locale is actually used.
Intl.Collator.supportedLocalesOf(["tr", "de", "sv", "ar", "ja"])can help identify supported requests, whilecollator.resolvedOptions()exposes resolved settings. - Normalization: Text can contain canonically equivalent sequences represented with different code points. Establish a consistent input policy and test composed and decomposed accented text. See the guide to Unicode normalization before using normalization in identifiers or search keys.
- Equality versus ordering: A comparison result of zero means “equivalent under these collator settings,” not necessarily “the same original string.” Preserve the original value and use a separate stable identifier.
- Rendering: Correct collation does not fix missing glyphs, fallback fonts, or bidirectional layout. For those issues, review CSS Unicode ranges and font fallback and bidirectional Unicode text.
- Runtime consistency: Browser and server runtimes may use different ICU or CLDR data. If sorted output is serialized, cached, or compared in tests, verify the environments together instead of assuming identical results.
Common mistakes
- Using
.sort()without a comparator: JavaScript then sorts values as strings by UTF-16 code units, which is rarely the desired multilingual order. - Calling
toLowerCase()as universal search logic: Case conversion alone does not define locale-aware equality, especially for Turkish and other language-sensitive cases. - Creating a new collator inside a comparator: Configure it once outside the sort and reuse
collator.compare. - Assuming one locale fits every user: “Alphabetical order” is a language and product decision, not a universal Unicode order.
- Using
numeric: truefor every code: It can be wrong when digits are fixed-format identifiers rather than quantities. - Treating collation as transliteration: Sorting does not turn Arabic, Japanese, or accented text into Latin text. If you need slugs or search aliases, define that transformation separately; the Unicode-aware search and slug guide covers the distinction.
- Ignoring grapheme boundaries: A visible character can contain multiple code points. Do not split user-visible text with naïve indexing when building advanced search or highlighting features.
When to revisit
Return to this checklist whenever you add a locale, change the language-selection model, introduce a new search field, or receive a report that alphabetical order differs between environments. It is also worth revisiting before seasonal planning cycles when catalogs, directories, or archived records are re-sorted in bulk.
Use this practical review sequence:
- Write down whether the operation is sorting, exact matching, autocomplete, or substring search.
- Choose representative fixtures for names, accents, numbers, case, and the scripts your users enter.
- Run the same fixtures in supported browsers and server runtimes.
- Record the locale, options, normalization policy, and expected order in tests.
- Check
resolvedOptions()after runtime or dependency changes. - Review the result with native-language stakeholders when the order affects a directory, ranking, or public-facing catalog.
For related Unicode debugging, see the guides to character escapes, Unicode property escapes, and multilingual line breaking. The central rule remains simple: make locale, comparison purpose, and equivalence rules explicit, then test them with the actual text your application handles.