Created
January 3, 2019 09:36
-
-
Save JamieDixon/0a39d7bb02416a6e97f085be6e00d7d6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const sortArrayByList = (fn, arr, order) => | |
arr.sort( | |
(item1, item2) => | |
order.indexOf(fn(item1)) > order.indexOf(fn(item2)) ? 1 : -1 | |
); | |
const selector = x => x.name; | |
const arr = [{ name: 'Jamie' }, { name: 'Simon' }, { name: 'Paul' }, { name: 'Stuart' }]; | |
const order = ['Stuart', 'Paul', 'Simon', 'Jamie']; | |
const result = sortArrayByList(selector, arr, order); | |
// [{ name: 'Stuart' }, { name: 'Paul' }, { name: 'Simon' }, { name: 'Jamie' }]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment