Skip to content

Instantly share code, notes, and snippets.

@nordyke
Created December 27, 2011 15:38
Show Gist options
  • Save nordyke/1524046 to your computer and use it in GitHub Desktop.
Save nordyke/1524046 to your computer and use it in GitHub Desktop.
Multi-sort on any number of Backbone Collection attributes. Sort happens from arguments[0] to arguments[n]
var MultiSortCollection = Backbone.Collection.extend({
/**
* Sort by supplied attributes. First param is sorted first, and
* last param is final subsort
* @param {String} sortAttributes
* @example collection.sortBy("last_name","first_name")
*/
sortBy : function(sortAttributes){
var attributes = arguments;
if(attributes.length){
this.models = this._sortBy(this.models,attributes);
}
},
/**
* Recursive sort
*/
_sortBy : function(models,attributes){
var attr,
that = this;
//base case
if(attributes.length === 1){
attr = attributes[0];
return _(models).sortBy(function(model){
return model.get(attr);
});
}
else{
attr = attributes[0];
attributes = _.last(attributes,attributes.length-1);
//split up models by sort attribute,
//then call _sortBy with remaining attributes
models = _(models).chain().
sortBy(function(model){
return model.get(attr);
}).
groupBy(function(model){
return model.get(attr);
}).
toArray().
value();
_(models).each(function(modelSet,index){
models[index] = that._sortBy(models[index],attributes);
});
return _(models).flatten();
}
}
});
var models = new MultiSortCollection,
model;
models.add([
{name : "Charlie",number: 5},
{name : "Billy", number: 7},
{name : "Albert",number: 1},
{name : "Charlie",number: 4}
]);
//collection order is [Charlie 5, Billy 7, Albert 1, Charlie 4]
models.sortBy("number","name"); //collection order is now [Albert 1, Charlie 4, Charlie 5, Billy 7]
models.sortBy("name","number"); //collection order is now [Albert 1, Billy 7, Charlie 4, Charlie 5]
model = new Backbone.Model({name : "Charlie",number:4.5});
console.log(models.sortIndex(model)); //returns 3
models.add(model); //colleciton order is now [Albert 1, Billy 7, Charlie 4, Charlie 4.5, Charlie 5]
@PaRoxUs
Copy link

PaRoxUs commented Jan 28, 2014

Great code man! Are you planing to implement a parameter for sort order?

I replaced line 40, 37, 26 with
var value = model.get(attr);
if ( isNumber( value ) ) {
return parseInt(value) * sortOrder;
} else {
return value.charCodeAt() * sortOrder;
}
but I don't get it to work properly. In some cases there is no problem but in others it returns the wrong order. One example is when the first attribute is an int between 0-5, that don't work, but when I try with an attribute that is an int between 10-60 it works. Do you got any insight what I'm doing wrong?

@shibinthomas
Copy link

Hey , great code . I want the data in the descending order can u help me?

@shibinthomas
Copy link

I have added this.models.reverse(); at the end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment