Created
March 21, 2011 23:39
-
-
Save tbeseda/880482 to your computer and use it in GitHub Desktop.
use with .sort() on an array of JSON objects.
This file contains 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
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