Skip to content

Instantly share code, notes, and snippets.

@Ivannnnn
Last active September 21, 2022 09:01
Show Gist options
  • Save Ivannnnn/6397c99fa9c87ce113f06c3fca944a0f to your computer and use it in GitHub Desktop.
Save Ivannnnn/6397c99fa9c87ce113f06c3fca944a0f to your computer and use it in GitHub Desktop.
function sortBy(objArr, key, ascDesc = "asc") {
const sortingFunc =
ascDesc === "desc" ? (a, b) => b[key] - a[key] : (a, b) => a[key] - b[key];
return [...objArr].sort(sortingFunc);
}
/*
const data = [
{ order: 1 },
{ order: 3 },
{ order: 7 },
]
USAGE: sortBy(data, 'order', 'desc')
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment