Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.