按优先级按属性排序 [英] Sorting by properties by priority
问题描述
我有要排序的项目,我的项目在模型上计算了属性,包括获胜
、bidding
、closed
, 和 观看
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屋!