Marked ordering with javascript

Asked

Viewed 35 times

0

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

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, there is the following message on the console

a.localeCompare is not a function

Rent has a tip to solve this problem with pure javascript?

  • a is a string?

  • Shouldn’t use a.nome_completo.localComparte(b.nome_completo)? For they are the strings that should be compared

1 answer

1


The problem is that a, in its code, it is an object, and the localeCompare is a class method string. Actually, you should compare the attribute nome_completo of objects a and b. So your code would look like this:

ordenaNomeCompleto: function(a,b){
    var ans = a.nome_completo.localeCompare(b.nome_completo);
    return ans;
}
  • @Mathuesreis Keeps replying on console: TypeError: a.nome_completo.localCompare is not a function

  • I wrote local instead of locale. Modify and try again, please, @Luissouza

  • It worked that is a beauty! Thank you so much for your help

  • I’ll ask another question, but, unbelieving ordination

Browser other questions tagged

You are not signed in. Login or sign up in order to post.