Reverse sort order with Backbone.js

前端 未结 15 983
不知归路
不知归路 2020-12-02 05:45

With Backbone.js I\'ve got a collection set up with a comparator function. It\'s nicely sorting the models, but I\'d like to reverse the order.

How can I sort the m

15条回答
  •  一个人的身影
    2020-12-02 06:29

    // For numbers, dates, and other number-like things
    var things = new Backbone.Collection;
    things.comparator = function (a, b) { return b - a };
    
    // For strings
    things.comparator = function (a, b) {
      if (a === b) return 0;
      return a < b ? -1 : 1;
    };
    

提交回复
热议问题