Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
48
rated 0 times [  50] [ 2]  / answers: 1 / hits: 47866  / 14 Years ago, wed, february 16, 2011, 12:00:00

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 models in descending order rather than ascending?


More From » backbone.js

 Answers
6

Well, you can return negative values from comparator. If we take, for example, the example from Backbone's site and want to reverse the order, it will look like this:



var Chapter  = Backbone.Model;
var chapters = new Backbone.Collection;

chapters.comparator = function(chapter) {
return -chapter.get(page); // Note the minus!
};

chapters.add(new Chapter({page: 9, title: The End}));
chapters.add(new Chapter({page: 5, title: The Middle}));
chapters.add(new Chapter({page: 1, title: The Beginning}));

alert(chapters.pluck('title'));

[#93721] Monday, February 14, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arlethjeanettep

Total Points: 506
Total Questions: 96
Total Answers: 79

Location: Liberia
Member since Tue, Mar 14, 2023
1 Year ago
;