Created
June 28, 2011 17:04
-
-
Save wyattdanger/1051605 to your computer and use it in GitHub Desktop.
alphabetize an array of objects on some shared key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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