Skip to content

Instantly share code, notes, and snippets.

@aindong
Created December 2, 2014 01:32
Show Gist options
  • Save aindong/b24b43f99e0b8d968813 to your computer and use it in GitHub Desktop.
Save aindong/b24b43f99e0b8d968813 to your computer and use it in GitHub Desktop.
Sort an array of object by passing a value
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;
}
}
@aindong
Copy link
Author

aindong commented Dec 2, 2014

usage People.sort(dynamicSort("Name"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment