Skip to content

Instantly share code, notes, and snippets.

@kutyel
Last active October 31, 2016 18:35
Show Gist options
  • Save kutyel/550194495556414aa61e59feabd4ad35 to your computer and use it in GitHub Desktop.
Save kutyel/550194495556414aa61e59feabd4ad35 to your computer and use it in GitHub Desktop.
Order an array given another array with its indices
const A = ["d", "a", "c", "b"];
const I = [3, 0, 2, 1];
A.reduce((x, y, i, arr) => x.concat(arr[I.indexOf(i)]), []);
// > ["a", "b", "c", "d"]
// Functional Programming Power!
const sortExternalFromArray = z => (x, y, i, arr) => x.concat(arr[z.indexOf(i)]);
// A more elegant/reusable approach
A.reduce(sortExternalFromArray(I), []);
// > ["a", "b", "c", "d"] // Still working!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment