5
votes

I have a simple comparator function on a Backbone.js collection.

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

This is the correct field to sort. It's a date field. I would like it to be sorted in desc order. Is there an easy way to reverse the order? Perhaps I should ditch this function and just sort the collection in prior to rendering it? Any ideas or tips are of course appreciated. Thanks all.

1

1 Answers

13
votes

If it's a JavaScript "Date" field, you could do this:

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

That'd return the negative of the timestamp, so that newer timestamps (bigger numbers) would come before older ones.

For a string-valued field this'd be tricky; you'd need to do something like "invert" the string character by character, or something.