Sorting cyrillic words with accents

Viewed 704

I experienced a problem with sorting array of strings written in the cyrilica (with accents). The following solution sorts the strings with and without diacritical marks separately at the beginning of the string.

function sorting(){
    let words = document.querySelectorAll(".dictionary");   
    let unsorted = [];
    let collator = new Intl.Collator('ru', {sensitivity: 'base'});
    words.forEach(s => {
        unsorted.push(s.textContent);
    });
    unsorted.sort(collator.compare);
    unsorted.forEach(s => {
        document.write("<p>" + s + "</p>");
    });
}

EDIT
This is array before sorting:
[..., 'áвтор', ..., 'авиабилéт', ..., 'янтáрный', ..., 'Áвстрия', 'áвтор', 'автостóп'];
this is after sorting:
["авиабилéт", "автостóп", ..., "янтáрный", ..., "Áвстрия", "áвтор"]
this is what I want to have
['авиабилéт', 'Áвстрия', 'áвтор', 'автостóп', ..., 'янтáрный', ...];

2 Answers

you have not provided sensitivity in options. Probably default value is 'variant'(I've tried in Chrome's console) while you need 'base'

Related