Created
August 26, 2015 14:17
-
-
Save michiel/8e4372ad553ce5a8444f to your computer and use it in GitHub Desktop.
Sort array of objects on key
This file contains hidden or 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 key = 'name'; | |
return objs.sort(function (a, b) { | |
if (a[key] > b[key]) { | |
return asc ? 1 : -1; | |
} else if (a[key] < b[key]) { | |
return asc ? -1 : 1; | |
} else { | |
return 0; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment