Skip to content

Instantly share code, notes, and snippets.

@tbeseda
Created March 21, 2011 23:39
Show Gist options
  • Save tbeseda/880482 to your computer and use it in GitHub Desktop.
Save tbeseda/880482 to your computer and use it in GitHub Desktop.
use with .sort() on an array of JSON objects.
var sort_by = function(field, reverse, primer){
reverse = (reverse) ? -1 : 1;
return function(a,b){
a = a[field];
b = b[field];
if (typeof(primer) != 'undefined'){
a = primer(a);
b = primer(b);
}
if (a<b) return reverse * -1;
if (a>b) return reverse * 1;
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment