Skip to content

Instantly share code, notes, and snippets.

@bialikover
Last active November 18, 2019 08:43
Show Gist options
  • Save bialikover/2e7edd47cfe6fd282dbda9c510fe5b5f to your computer and use it in GitHub Desktop.
Save bialikover/2e7edd47cfe6fd282dbda9c510fe5b5f to your computer and use it in GitHub Desktop.
Higher order function / curry fn to sort objects
const by = (key) => {
const sortNumbers = (a, b) => a - b;
const sortLexico = (a, b) => a.localeCompare(b)
return (a, b) => {
return isNaN(a[key])
?
sortLexico(a[key], b[key])
:
sortNumbers(a[key], b[key])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment