Valentine
Valentine

Reputation: 161

Vue.js - Sorting a list in descending order

I want to display a sorted list using the value I get from my DataObject:

<div class="recommendation" v-for="l in list" :key="`_${l.id}`">

My list is not currently ordered:

export default {
  name: "HelloWorld",
  data() {
    return {
      list: []
    };
  },
  mounted: function() {
    let self = this;
    backend.matches().then(function(resp) {
      self.list = resp.data.listings;
    });
  },
  computed: {
    
  }
};

I want to order my list with my l.weight value in descending order.

What would be the best approach to order my list?

Upvotes: 1

Views: 2597

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

Try to use the sort method :

    backend.matches().then(function(resp) {
      self.list = resp.data.listings;
      self.list.sort((a,b)=>a.weight<b.weight?1:-1) 
    });

Upvotes: 2

Related Questions