Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created March 11, 2009 21:31
Show Gist options
  • Select an option

  • Save subtleGradient/77756 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/77756 to your computer and use it in GitHub Desktop.
Array.makeSortByKey = function makeSortByKey(key){
return Array.makeSortByKey.cache[key]
||(Array.makeSortByKey.cache[key] = function sortBy(a,b){
if (a[key] == b[key]) return 0;
return (a[key] < b[key]) ? -1 : 1;
});
};
Array.makeSortByKey.cache = {};
myArray.sort(Array.makeSortByKey('id')); // -> array sorted by 'id'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment