Sorting an object array in VueJs is not reading the function

227 Views Asked by At

I'm retrieving an object array from hackernews and trying to sort it by score here but nothing happens.

The console outputs the exact same array under unsorted and sorted.

What am I doing wrong here:

created: function (){
    axios.get('https://hacker-news.firebaseio.com/v0/beststories.json')
    .then((response) => {
      let results = response.data.slice(0,10);
      console.log("Unsorted: " + results);

      let sortedStories = results.sort(function compare(a, b) {
        if (a.score > b.score)
          return -1;
        if (a.score < b.score)
          return 1;
        return 0;
      });

      console.log("Sorted: " + sortedStories);
    })
});
1

There are 1 best solutions below

0
tauzN On

The provided API url returns a list of sorted numbers. It's already sorted.