Skip to content

Instantly share code, notes, and snippets.

@lanqy
Last active December 13, 2015 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanqy/4975044 to your computer and use it in GitHub Desktop.
Save lanqy/4975044 to your computer and use it in GitHub Desktop.
sort
//http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-javascript-objects
// demo http://jsfiddle.net/dFNva/1/
var sort_by = function(field, reverse, primer){
var key = function (x) {return primer ? primer(x[field]) : x[field]};
return function (a,b) {
var A = key(a), B = key(b);
return ((A < B) ? -1 :
(A > B) ? +1 : 0)) * [-1,1][+!!reverse];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment