Sort accents by language and put 2 before 10.
dotnet add package RecursiveAI.OneEcmaRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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, then dotnet run.
Prints:
Ana 1, Éva 2, Éva 10The full program
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:
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.
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#
- Normalize JavaScript Unicode text in C#
- Format JavaScript dates by locale and time zone in C#
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 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 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 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.