Skip to content

Instantly share code, notes, and snippets.

@whalemare
Created July 15, 2019 10:12
Show Gist options
  • Save whalemare/70110b739e09acf425d2a0a4ffaccf9a to your computer and use it in GitHub Desktop.
Save whalemare/70110b739e09acf425d2a0a4ffaccf9a to your computer and use it in GitHub Desktop.
Simple .sortBy implementation for JS
function compare(x, y) {
return x > y
}
/**
* @param {any[]} array list of items
* @param {() => boolean} f function selector
*/
function sortBy(array, f) {
return array.sort((x, y) => compare(f(x), f(y)))
}
export const SomeEnum = {
CRITICAL: 'CRITICAL',
CONTINUE: 'CONTINUE',
READY: 'READY',
order: item => {
return Object.values(PlanToothStatus).findIndex(value => value === item)
},
}
const result = sortBy(Object.values(SomeEnum), element => SomeEnum.order(element))
// Output will be
// ['CRITICAL', 'CONTINUE', 'READY', [function: order]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment