I am using Backbone.js to render a list of items e.g Books. After the list is rendered, there are options for user to sort them. So if user clicks on Sort By Title, or Sort
This way works well and enables sorting by all attributes dynamically and handles ascending or descending:
var PhotoCollection = Backbone.Collection.extend({
model: Photo,
sortByField: function(field, direction){
sorted = _.sortBy(this.models, function(model){
return model.get(field);
});
if(direction === 'descending'){
sorted = sorted.reverse()
}
this.models = sorted;
}
});