---
title: "Sort JavaScript strings by locale in C#"
url: https://recursiveai.org/oneecma/cookbook/sort-javascript-strings-with-locale-collation/
description: "Use Intl.Collator in JavaScript hosted by C# to sort accented names and put Item 2 before Item 10. See how OneEcma resolves unsupported locales."
version: "1.1.1"
---

# Sort JavaScript strings by locale in C#

**OneEcma** is a free, open-source JavaScript interpreter for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneEcma) · [GitHub](https://github.com/recursive-ai-oss/oneecma-csharp) · [Docs](https://recursiveai.org/oneecma/docs/) · [Download example (.zip)](https://recursiveai.org/oneecma/cookbook/sort-javascript-strings-with-locale-collation/sort-javascript-strings-with-locale-collation.zip)

Sort accents by language and put 2 before 10.

```shell
dotnet add package RecursiveAI.OneEcma
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneEcma;
Console.WriteLine(new ScriptEngine()
  .Evaluate("""
const collator = new Intl.Collator('fr',
  { numeric: true });
['Éva 10', 'Éva 2', 'Ana 1']
  .sort(collator.compare).join(', ');
""").AsString);
```

Unzip [the example](https://recursiveai.org/oneecma/cookbook/sort-javascript-strings-with-locale-collation/sort-javascript-strings-with-locale-collation.zip), then `dotnet run`.

Prints:

```text
Ana 1, Éva 2, Éva 10
```

## The full program

```csharp
using RecursiveAI.OneEcma;
var engine = new ScriptEngine();
Console.WriteLine(engine.Evaluate("""
const names = ['Zoé', 'Éva 10', 'Éva 2', 'Ana'];
const options = { numeric: true };
const collator = new Intl.Collator('fr', options);
const fallback = new Intl.Collator(['ja-JP', 'fr'], options);
const onlyJapanese = new Intl.Collator('ja-JP', options);
[
  names.sort(collator.compare).join(', '),
  'ja-JP then fr: ' + fallback.resolvedOptions().locale,
  'ja-JP only: ' + onlyJapanese.resolvedOptions().locale
].join('\n');
""").AsString);
```

`dotnet run` prints:

```text
Ana, Éva 2, Éva 10, Zoé
ja-JP then fr: fr
ja-JP only: en-US
```

The default host resolves an unsupported request to `en-US` if the locale list has no supported choice. An invalid tag such as `en_US` instead raises JavaScript `RangeError` (a `JsException` if it reaches C#).

## Variations

With `sensitivity: 'base'`, an accent alone doesn’t distinguish these two names. Replace the JavaScript inside `Evaluate` with the code below. It prints `true`.

```javascript
const names = ['Éva', 'Eva'];
const collator = new Intl.Collator('fr', {
  numeric: true, sensitivity: 'base'
});
String(collator.compare(...names) === 0);
```

## Related recipes

- [Split JavaScript text into graphemes in C#](https://recursiveai.org/oneecma/cookbook/segment-javascript-text-into-graphemes/)
- [Normalize JavaScript Unicode text in C#](https://recursiveai.org/oneecma/cookbook/normalize-javascript-unicode-text/)
- [Format JavaScript dates by locale and time zone in C#](https://recursiveai.org/oneecma/cookbook/format-javascript-dates-by-locale/)

## The short answer

Create an `Intl.Collator` with the user’s locale and `{ numeric: true }`, then pass its `compare` function to the array’s `sort` method. OneEcma’s `ScriptEngine` runs the JavaScript in your C# process. In the example, French collation orders the names and compares the digits in the labels by value, so `Éva 2` comes before `Éva 10`.

## How do you sort a JavaScript array by the numbers in its labels?

An ordinary string sort sees the first digit `1` in `10` before `2`. With numeric collation, a run of decimal digits is compared by value, and leading zeros don’t count. OneEcma’s [text services documentation](https://recursiveai.org/oneecma/docs/text-services/) describes this as “A run of decimal digits (general category Nd, any script) compares by value; leading zeros do not count; digits of any length.”

That is useful for room numbers, sections, and filenames, but it isn’t a numeric parser. For signed values, decimals, or amounts you’ve already parsed into numeric fields, sort on the value rather than on the label. For display names, use `sensitivity` to decide which differences count: the variation uses `base` so Éva and Eva compare equal. That doesn’t make the strings identical, and you shouldn’t use the comparison alone to decide whether they belong to the same person. See [MDN’s `Intl.Collator` reference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator) for how to interpret the sign of `compare`.

## Which locales does `Intl.Collator` support in OneEcma?

OneEcma ships locale data for `en-US`, `en-GB`, `es` (and `es-US`), `fr`, `de`, and `pt-BR`. Pass a list in preference order if several choices are acceptable. `ja-JP` isn’t in the embedded data, so the example gets `fr` with the list and `en-US` with `ja-JP` alone. The [Intl documentation](https://recursiveai.org/oneecma/docs/intl/#known-gaps) describes locales outside that set: “the lookup falls back to the default locale.”

Inspect `resolvedOptions().locale` to record what the collator chose. If you need to distinguish an unsupported request from a fallback, `Intl.Collator.supportedLocalesOf` tells you which of your requests the engine can satisfy without falling back to the default. The locale you request matters for sorting text even when the number rule stays the same.
