[Vuejs]-Array.sort(function(a, b) {return b.value – a.value) not working

0👍

sort() method return value has 3 options: 1, -1, 0

top.sort(function (a, b) { return a.overallScore > b.overallScore ? 1 : a.overallScore < b.overallScore ? -1 : 0 });

0👍

change sort function to
a.overallScore – b.overallScore

let top = [
  {overallScore: 1},
  {overallScore: 2},
    {overallScore: 8},
  {overallScore: 12},
    {overallScore: 4},
  {overallScore: 5},
    {overallScore: 3},
  {overallScore: 29},
]

top.sort(function(a, b)
         {
          return (a.overallScore - b.overallScore);}
        );

console.log(top)

https://jsbin.com/zofogar/edit?html,js,output

Leave a comment