按属性按优先排序 [英] Sorting by properties by priority

查看:92
本文介绍了按属性按优先排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要排序的项目,我的项目在模型上计算了属性,包括获胜出价关闭观看



这些属性是真的还是假的



我想对项目进行排序,以便所有的获胜项目都是第一个,然后是所有的投标项目,然后是所有的观看项目,然后所有关闭的项目。



我该如何实现?我需要在一个排序函数中执行所有操作,还是可以指定多个函数进行排序?



我已经尝试过获胜了,但我不确定从这里去哪里

  sortedItems:Ember.computed.sort('filteredItems',function(a,b){
if(a.get('winner')== true&& a.get('winner')!= b.get('winner')){
return -1;
}
if(a.get('winner')== b.get('winner')){
return 0;
}
if(a.get 'win')== false&& a.get('winner')!= b.get('winner')){
return 1;
}
}),


解决方案

我建议使用这样的排序功能。

 函数(a,b){
var status = {获胜:1 ,招标:2,关闭:3,看:4};
return status [a.bidStatus] - status [b.bidStatus];
}


I have items that I'm wanting to sort, my items have computed properties on the model that include winning, bidding, closed, and watching

each of these properties is true or false

I want to sort the items so that all the winning items are first, then all the bidding items, then all the watching items, then all the closed items.

How might I achieve this? Do I need to do it all in one sort function or can I specify multiple functions to sort by?

I've tried this for winning, but I'm not sure where to go from here

sortedItems: Ember.computed.sort('filteredItems',function(a,b){
  if(a.get('winning') == true && a.get('winning') != b.get('winning')){
    return -1;
  }
  if(a.get('winning') == b.get('winning')){
    return 0;
  }
  if(a.get('winning') == false && a.get('winning') != b.get('winning')){
    return 1;
  }
}),

解决方案

I suggest to use a sorting function like this. It sorts by grouping the bid status.

function (a, b) {
    var status = { winning: 1, bidding: 2, closed: 3, watching: 4 };
    return status[a.bidStatus] - status[b.bidStatus];
}

这篇关于按属性按优先排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆