whyAto8
whyAto8

Reputation: 1670

Ember - computed sort

I am using the below code for sorting a list.

sortOptions: ['amount:desc','place']
Ember.computed.sort('model',sortOptions)

The key "amount" is basically a number, but in JSON "model", its coming as a string. So, when I ran this code, it wasn't sorting by amount, but when I modified the JSON to convert that amount string to amount number, that worked. Is this correct behavior of Ember computed sort?

Upvotes: 0

Views: 1148

Answers (1)

murli2308
murli2308

Reputation: 3002

you can use custom function with the Ember.computed.sort which can solve your problem

I believe you get string as amount from JSON and you want to sort it as descending order.

// using a custom sort function
Ember.computed.sort('model', function(a, b){
  if (a.amount > b.amount) {
    return -1;
  } else if (a.amount < b.amount) {
    return 1;
  } else {
    return 0;
  }
})

Upvotes: 1

Related Questions