Created
August 17, 2018 08:58
-
-
Save bezenson/a95b15bdedb9ce16e0c4643ab1caab31 to your computer and use it in GitHub Desktop.
Sort array by original array
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
import { curry, pipe, sort } from 'ramda'; | |
const sortListByPattern = curry((pattern, list) => pipe( | |
sort((a, b) => pattern.indexOf(a) - pattern.indexOf(b)), | |
)(list)); | |
const pattern = ['s', 'o', 'm', 'e', 'v', 'a', 'l']; | |
const list = ['o', 'a', 'e', 'v', 'l', 's']; | |
// Example: | |
sortListByPattern(pattern, list); | |
// or | |
sortListByPattern(pattern)(list); | |
// will return: | |
// ['s', 'o', 'e', 'v', 'a', 'l'] ('m' not exists in given array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment