Last active
March 29, 2016 19:02
-
-
Save bogvsdev/f13fff262924dc76b38cc2e89134acf6 to your computer and use it in GitHub Desktop.
js sorting of object
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){ | |
var key = primer ? | |
function(x) {return primer(x[field])} : | |
function(x) {return x[field]}; | |
reverse = !reverse ? 1 : -1; | |
return function (a, b) { | |
return a = key(a), b = key(b), reverse * ((a > b) - (b > a)); | |
} | |
} | |
posts.sort(sort_by('post_id', true, parseInt)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment