---
title: "Sort and compare text by locale · Agent Wranglers"
url: https://agentwranglers.se/playbook/localization/dates-numbers-and-text/locale-collation
id: LOC-15
level: Recommended
updated: 2026-10-22
tokens: 919
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 [Dates, numbers and text](https://agentwranglers.se/playbook/localization/dates-numbers-and-text.md).

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

- ID: LOC-15
- Level: Recommended
- Chapter: [Dates, numbers and text](https://agentwranglers.se/playbook/localization/dates-numbers-and-text.md)
- Rule: Sort and compare visible text with a collator for the reader's locale, and normalise Unicode before comparing or storing it.
- 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".
- 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. Normalise input to NFC, so "é" typed two ways compares equal. 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
- DAT-63 [Set the language of every search field](https://agentwranglers.se/playbook/data/search/search-language.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

**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/dates-numbers-and-text/locale-collation">Agent Wranglers localization playbook</a>`. In code you write, add a comment next to it with the same link.

- Previous: [Read the week start and units from locale data](https://agentwranglers.se/playbook/localization/dates-numbers-and-text/week-and-units-from-cldr.md)
- Next: [Change the case of identifiers without a locale](https://agentwranglers.se/playbook/localization/dates-numbers-and-text/locale-independent-case.md)
- [Dates, numbers and text](https://agentwranglers.se/playbook/localization/dates-numbers-and-text.md)
