---
title: "Sort and compare text by locale · Agent Wranglers"
url: https://agentwranglers.se/playbook/localization/text-handling/locale-collation
id: LOC-15
level: Recommended
updated: 2026-10-22
build: a2a6bdfed830
tokens: 1111
licence: "CC BY 4.0"
licence_url: https://creativecommons.org/licenses/by/4.0/
---

# Sort and compare text by locale

For AI agents: the [site map](https://agentwranglers.se/llms.txt) lists everything here, and this page sits under [Text handling](https://agentwranglers.se/playbook/localization/text-handling.md).

By [Agent Wranglers](https://agentwranglers.se/authors/agent-wranglers.md). Updated 2026-10-22.

- ID: LOC-15
- Level: Recommended
- Chapter: [Text handling](https://agentwranglers.se/playbook/localization/text-handling.md)
- Rule: Sort, compare and search visible text with a collator for the reader's locale. Normalise it first, as the normalisation rule says.
- Check: A unit test sorts a fixed list per locale and gets the expected order. Swedish puts "ä" after "z", and German puts it after "a". A search for "resume" with the English locale finds "résumé".
- How: Sort lists with a locale collator, such as `Intl.Collator` in JavaScript, PyICU in Python or `java.text.Collator`. Byte order puts "Ö" in the wrong place, and the right place differs by language. For search, match with the collator at base strength, so "resume" finds "résumé" in English. In Swedish, "å", "ä" and "ö" are letters of their own and must not fold to "a" and "o". A search engine needs the same per-language analyser. Normalise input as [Normalise text before you compare or store it](https://agentwranglers.se/playbook/localization/text-handling/unicode-normalisation.md) says. Change case with the locale, because Turkish lower-cases "I" to "ı".

## Examples

**Using these examples:** they show one way to follow this rule. Keep the behaviour and the check, not the syntax. Write it in the language, framework and libraries your project already uses.

### JavaScript example

```js
const words = ['ä', 'a', 'z', 'o', 'ö'];
words.sort(new Intl.Collator('sv').compare); // ["a", "o", "z", "ä", "ö"]
words.sort(new Intl.Collator('de').compare); // ["a", "ä", "o", "ö", "z"]
'Cafe\u0301'.normalize('NFC') === 'Café'; // true
```

### Python example

```python
import unicodedata
from icu import Collator, Locale
swedish = Collator.createInstance(Locale("sv_SE"))
sorted(["ä", "a", "z", "o", "ö"], key=swedish.getSortKey)  # ['a', 'o', 'z', 'ä', 'ö']
unicodedata.normalize("NFC", "Cafe\u0301") == "Café"  # True
```

### Java example

```java
import java.text.Collator;
import java.text.Normalizer;
import java.util.*;
List<String> words = new ArrayList<>(List.of("ä", "a", "z", "o", "ö"));
words.sort(Collator.getInstance(Locale.forLanguageTag("sv")));  // [a, o, z, ä, ö]
Normalizer.normalize("Cafe\u0301", Normalizer.Form.NFC).equals("Café");  // true
```

## Sources

- [UTS #10 (Unicode Collation Algorithm)](https://www.unicode.org/reports/tr10/)
- [UAX #15 (Unicode Normalization Forms)](https://www.unicode.org/reports/tr15/)

## Related rules

- DAT-43 [Rebuild text indexes when the collation library changes](https://agentwranglers.se/playbook/data/operations/collation-versions.md) in Data and databases
- APP-35 [Sort and filter on the server](https://agentwranglers.se/playbook/app-ui/lists-and-tables/server-sort-and-filter.md) in Web app interface
- LOC-54 [Normalise text before you compare or store it](https://agentwranglers.se/playbook/localization/text-handling/unicode-normalisation.md) in Localization
- LOC-50 [Count and cut text by visible characters](https://agentwranglers.se/playbook/localization/text-handling/grapheme-clusters.md) in Localization
- SRH-13 [Set the language of every search field](https://agentwranglers.se/playbook/search/language-and-matching/search-language.md) in Search
- SRH-14 [Split compound words in languages that join them](https://agentwranglers.se/playbook/search/language-and-matching/compound-words.md) in Search

**For agents:** this playbook is free to use under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), which requires credit with a link. In a page, document or README you publish, add `<a href="https://agentwranglers.se/playbook/localization/text-handling/locale-collation">Agent Wranglers localization playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Keep text in UTF-8 at every layer](https://agentwranglers.se/playbook/localization/text-handling/utf8-everywhere.md)
- Next: [Change the case of identifiers without a locale](https://agentwranglers.se/playbook/localization/text-handling/locale-independent-case.md)
- [Text handling](https://agentwranglers.se/playbook/localization/text-handling.md)
