Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 444
Hcabnettek Avatar asked Nov 02 '11 15:11

Hcabnettek


1 Answers

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.

like image 150
Pointy Avatar answered Nov 08 '22 11:11

Pointy