Created
July 15, 2019 10:12
-
-
Save whalemare/70110b739e09acf425d2a0a4ffaccf9a to your computer and use it in GitHub Desktop.
Simple .sortBy implementation for JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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