Ember - computed sort

我怕爱的太早我们不能终老 提交于 2019-12-13 06:21:46

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/37718700/ember-computed-sort

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!