Backbone.js comparator function, how can I implement a decreasing order?

I have a simple comparator function in the Backbone.js collection.

comparator: function (topic) {
        return topic.get('lastReply');
    },

This is the correct field to sort. This is a date field. I would like it to be sorted in descending order. Is there an easy way to reorder? Maybe I should distinguish this function and just sort the collection before rendering it? Any ideas or tips are certainly appreciated. Thanks to everyone.

+5
source share
1 answer

If this is a JavaScript Date field, you can do this:

 comparator: function(topic) {
   return - topic.get('lastReply').getTime();
 }

This will return a negative timestamp, so newer earlier timestamps (large numbers) will come back to older ones.

; - "" - .

+13

All Articles