Skip to content

Instantly share code, notes, and snippets.

@leocaseiro
Forked from dreadjr/example.js
Created June 8, 2016 05:50
Show Gist options
  • Select an option

  • Save leocaseiro/c5280b7cc492da98cbc9c86d1c110fb9 to your computer and use it in GitHub Desktop.

Select an option

Save leocaseiro/c5280b7cc492da98cbc9c86d1c110fb9 to your computer and use it in GitHub Desktop.
Lodash / Underscore sort object keys. Like _.sortBy(), but on keys instead of values, returning an object, not an array. Defaults to alphanumeric sort.
_.mixin({
'sortKeysBy': function (obj, comparator) {
var keys = _.sortBy(_.keys(obj), function (key) {
return comparator(obj[key], key);
});
return _.object(keys, _.map(keys, function (key) {
return obj[key];
}));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment