Skip to content

Instantly share code, notes, and snippets.

@lamchau
Created May 26, 2015 02:34
Show Gist options
  • Save lamchau/7afa72ae669553adca2a to your computer and use it in GitHub Desktop.
Save lamchau/7afa72ae669553adca2a to your computer and use it in GitHub Desktop.
function orderByProperty(prop) {
var args = [].slice.call(arguments, 1);
return function (a, b) {
var propA = a[prop];
var propB = b[prop];
var result;
if (typeof propA == 'string' || typeof propB == 'string') {
result = String(propA).localeCompare(propB);
} else {
result = propA - propB;
}
if (result === 0 && arguments.length) {
return orderByProperty.apply(null, args)(a, b);
}
return result;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment