-2

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);
    })
});
zcoop98
  • 2,590
  • 1
  • 18
  • 31
zevex
  • 39
  • 3
  • 2
    This API returns `array of numbers` not an `array of objects`. Hence, there is no score property exist. – Debug Diva Apr 28 '22 at 12:04
  • Hi @RohìtJíndal Im pretty sure its an object. And that function stored in ``sortedStories`` works if I call I put it under a computed hook. But here inline it does not do anything. Im thinking it might be a syntax error. This is what the call returns: ``{by: "marban", descendants: 3644, id: 31153277,…} by: "marban" descendants: 3644 id: 31153277 score: 2479 time: 1650887900 title: "Twitter set to accept Musk's $43B offer – sources" type: "story" url: "https://www.reuters.com/technology/exclusive-twitter-set-accept-musks-best-final-offer-sources-2022-04-25/"`` – zevex Apr 28 '22 at 12:41

1 Answers1

0

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

tauzN
  • 5,162
  • 2
  • 16
  • 21