Created
December 2, 2014 01:32
-
-
Save aindong/b24b43f99e0b8d968813 to your computer and use it in GitHub Desktop.
Sort an array of object by passing a value
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
function dynamicSort(property) { | |
var sortOrder = 1; | |
if(property[0] === "-") { | |
sortOrder = -1; | |
property = property.substr(1); | |
} | |
return function (a,b) { | |
var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0; | |
return result * sortOrder; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage People.sort(dynamicSort("Name"));