Created
October 28, 2013 19:13
-
-
Save pjdietz/7202771 to your computer and use it in GitHub Desktop.
Return a compare function to sort objects by a given property
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
/** | |
* 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