Enhanced ordering with javascript

0

I have the following function to sort, but, does not sort the accented words correctly

ordenaNomeCompleto: function(a,b){
    var ans = 0;
    if(a.nome_completo < b.nome_completo) ans = -1
    if(a.nome_completo > b.nome_completo) ans = 1
   return ans;
    },

Searching the Internet I found this method:

ans  = a.localeCompare(b)

But, you have the following message in the console

a.localeCompare is not a function

Is there a hint to solve this problem with pure javascript?

    
asked by anonymous 08.07.2018 / 00:27

1 answer

1

The problem is that a , in your code, is an object, and localeCompare is a method of class string . In fact, you should compare the nome_completo attribute of the a and b objects. So your code looks like this:

ordenaNomeCompleto: function(a,b){
    var ans = a.nome_completo.localeCompare(b.nome_completo);
    return ans;
}
    
08.07.2018 / 00:47