Skip to main content

Function: localeCompare()

localeCompare<T>(fn, locale): Comparator<T>

Defined in: compare/advanced/index.ts:95

Creates a locale-aware string comparator.

Uses the browser/Node.js locale system for culturally appropriate sorting. Handles accented characters, special characters, and language-specific rules.

Type Parameters

T

T

Parameters

fn

ComparableExtractor<T, string>

Function to extract the string value

locale

string

BCP 47 language tag (e.g., 'en-US', 'de-DE', 'fr-FR')

Returns

Comparator<T>

A locale-aware comparator

Example

interface City {
name: string
country: string
}

const cities = [
{ name: 'Zürich', country: 'Switzerland' },
{ name: 'Berlin', country: 'Germany' },
{ name: 'München', country: 'Germany' },
{ name: 'Åarhus', country: 'Denmark' }
]

// German locale (handles umlauts correctly)
cities.sort(localeCompare(c => c.name, 'de-DE'))
// => [Åarhus, Berlin, München, Zürich]

// French locale
const names = [
{ name: 'Étienne' },
{ name: 'Eric' },
{ name: 'Émile' }
]
names.sort(localeCompare(n => n.name, 'fr-FR'))
// => [Émile, Eric, Étienne]

// English locale
const words = [
{ word: 'résumé' },
{ word: 'resume' },
{ word: 'result' }
]
words.sort(localeCompare(w => w.word, 'en-US'))

See

  • byString - for more string comparison options
  • caseInsensitive - for simple case-insensitive sorting