HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Compare strings regardless of case and accent

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptregardlesscaseandaccentstringscompare

Problem

Comparing and sorting JavaScript strings is rather common. Usually, the use of String.prototype.localeCompare() is sufficient for sorting arrays of strings in most scenarios. Dealing with accents and cases can get tricky, however, and lead to unexpected results. This is where Intl.Collator comes into play, an object used for language-sensitive string comparison. Using Intl.Collator.prototype.compare(), you can sort strings regardless of their case or accent and it can even accept a locale argument.

Solution

const arr = ['ä', 'a', 'b', 'A', 'B', 'Å'];

const localeCompare = (a, b) => a.localeCompare(b);
const collator = new Intl.Collator();
const deCollator = new Intl.Collator('de');
const svCollator = new Intl.Collator('sv');

arr.sort(localeCompare);      // ['a', 'A', 'Å', 'ä', 'b', 'B']
arr.sort(collator.compare);   // ['a', 'A', 'Å', 'ä', 'b', 'B']
arr.sort(deCollator.compare); // ['a', 'A', 'Å', 'ä', 'b', 'B']
arr.sort(svCollator.compare); // ['a', 'A', 'b', 'B', 'Å', 'ä']

Code Snippets

const arr = ['ä', 'a', 'b', 'A', 'B', 'Å'];

const localeCompare = (a, b) => a.localeCompare(b);
const collator = new Intl.Collator();
const deCollator = new Intl.Collator('de');
const svCollator = new Intl.Collator('sv');

arr.sort(localeCompare);      // ['a', 'A', 'Å', 'ä', 'b', 'B']
arr.sort(collator.compare);   // ['a', 'A', 'Å', 'ä', 'b', 'B']
arr.sort(deCollator.compare); // ['a', 'A', 'Å', 'ä', 'b', 'B']
arr.sort(svCollator.compare); // ['a', 'A', 'b', 'B', 'Å', 'ä']

Context

From 30-seconds-of-code: compare-string-case-accent-insensitive

Revisions (0)

No revisions yet.