How to sort array of strings disregarding accents?

15

If I have an array like this:

exemplo = ["Árvore", "Casa", "Computador", "É", "Poste", "Pássaro", "Índia", "Ar", "Ásia"]

The exemplo.sort() considers the accentuation of the words to sort, putting the accented ones last:

exemplo.sort()
["Ar", "Casa", "Computador", "Poste", "Pássaro", "Árvore", "Ásia", "É", "Índia"]

How to get the array sorted by disregarding the accents?

    
asked by anonymous 20.05.2014 / 23:45

1 answer

14

Use the localeCompare, according to the documentation found here: link

var items = ["Cátodo", "caule", "casca"];
items.sort(function (a, b) {
    return a.localeCompare(b);
});

The result is equivalent to:

[ "casca", "Cátodo", "caule" ]
    
20.05.2014 / 23:57