zevex
zevex

Reputation: 39

Sorting an object array in VueJs is not reading the function

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);
    })
});

Upvotes: -2

Views: 241

Answers (1)

tauzN
tauzN

Reputation: 7021

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

Upvotes: 0

Related Questions