Skip to content

Instantly share code, notes, and snippets.

@erkanzileli
Created January 23, 2019 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erkanzileli/2c45a8fd9ecf87818fcaf4c824f16140 to your computer and use it in GitHub Desktop.
Save erkanzileli/2c45a8fd9ecf87818fcaf4c824f16140 to your computer and use it in GitHub Desktop.
order an array by selected key and choose ascending or descending
function orderByKey (arr, key, asc) {
return arr.sort((a, b) => {
if (a[key] < b[key]) {
return asc === true ? -1 : 1
}
if (a[key] > b[key]) {
return asc === true ? 1 : -1
}
return 0
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment