Skip to content

Instantly share code, notes, and snippets.

@icetee
Created November 25, 2016 15:48
Show Gist options
  • Save icetee/a34c0361c8902a3aa1f5c0d310026d3d to your computer and use it in GitHub Desktop.
Save icetee/a34c0361c8902a3aa1f5c0d310026d3d to your computer and use it in GitHub Desktop.
Underscore extend recursive sort (Thanks: Craig Shoemaker)
_.sortRecursive = function(array, propName, propChild) {
array.forEach(function(item) {
var keys = _.keys(item);
keys.forEach(function(key) {
if (_.isArray(item[key])) {
item[key] = _.sortRecursive(item[key], propName);
} else if (_.isObject(item[key])) {
if (item[key].hasOwnProperty(propChild)) {
item[key][propChild] = _.sortRecursive(item[key][propChild], propName, propChild);
}
}
});
});
return _.sortBy(array, propName);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment