Skip to content

Instantly share code, notes, and snippets.

@lloc
Created June 6, 2014 12:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lloc/cdcd0d07ea9526eeff49 to your computer and use it in GitHub Desktop.
Save lloc/cdcd0d07ea9526eeff49 to your computer and use it in GitHub Desktop.
Sort an array-like object with underscore.js
var i, key, pairs,
obj = {'4': 'alpha', '2': 'beta', '5': 'gamma', '1': 'delta', '3': 'epsilon'};
for (key in obj) {
console.log(key + ': ' + obj[key]);
}
pairs = _.pairs(obj);
obj = _.sortBy(pairs, function (item) { return item[1]; });
for (i = 0; i < obj.length; i++) {
console.log(obj[i][0] + ': ' + obj[i][1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment