Skip to content

Instantly share code, notes, and snippets.

@bezenson
Created August 17, 2018 08:58
Show Gist options
  • Save bezenson/a95b15bdedb9ce16e0c4643ab1caab31 to your computer and use it in GitHub Desktop.
Save bezenson/a95b15bdedb9ce16e0c4643ab1caab31 to your computer and use it in GitHub Desktop.
Sort array by original array
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