Sorting by properties by priority

前端 未结 2 1537
旧时难觅i
旧时难觅i 2021-01-23 07:54

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

相关标签:
2条回答
  • 2021-01-23 08:39
    consider winning = 0, bidding = 1, watching = 2, closed = 3
    
        var arrayOfBids = [a, b, c, d, e, f];
    
        arrayOfBids.sort(function(bid1, bid2) {
    
            return getBidStatusVal(bid1.status) - getBidStatusVal(bid2.status);
    
            function getBidStatusVal(bidStatus) {
               switch(bidStatus) {
                 case "winning": return 0;
                 case "bidding": return 1;
                 case "watching": return 2;
                 case "closed": return 3;
                 default: return 4;
               }
            }
        });
    
    0 讨论(0)
  • 2021-01-23 08:46

    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];
    }
    
    0 讨论(0)
提交回复
热议问题