Skip to content

Instantly share code, notes, and snippets.

@pjdietz
Created October 28, 2013 19:13
Show Gist options
  • Save pjdietz/7202771 to your computer and use it in GitHub Desktop.
Save pjdietz/7202771 to your computer and use it in GitHub Desktop.
Return a compare function to sort objects by a given property
/**
* Return a compare function to sort objects by a given property.
* @param {string} property Name of the property to sort by.
* @return {function} A compare function
*/
function compareByProperty(property) {
return function (a, b) {
if (a !== undefined && b !== undefined && property in a && property in b) {
if (a[property] !== b[property]) {
return a[property] < b[property] ? -1 : 1;
}
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment