Skip to content

Instantly share code, notes, and snippets.

@wyattdanger
Created June 28, 2011 17:04
Show Gist options
  • Save wyattdanger/1051605 to your computer and use it in GitHub Desktop.
Save wyattdanger/1051605 to your computer and use it in GitHub Desktop.
alphabetize an array of objects on some shared key
function alphabetizeOn(arr, fn) {
var tmp = [],
final = [];
arr.forEach(function (item) {
var key = fn(item);
tmp.push(key);
});
tmp.sort();
tmp.forEach(function (item) {
arr.forEach(function (subitem) {
if (fn(subitem) === item) {
final.push(subitem);
}
});
});
return final;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment