Skip to content

Instantly share code, notes, and snippets.

@ceane
Last active March 25, 2016 22:52
Show Gist options
  • Save ceane/217ea04fc2759ecea21d to your computer and use it in GitHub Desktop.
Save ceane/217ea04fc2759ecea21d to your computer and use it in GitHub Desktop.
Simple sort
/**
* Sort an array by another array
* i.e. (blue, red, yellow, orange) -> (red, orange, yellow blue)
*
* @param {Array} a - Array to be sorted
* @param {Array} b - Array of order
*/
function asort(a, b) {
let z = [...a];
for (let i = 0, len = a.length; i < len; i++) {
let v = b[i];
let l = z.indexOf(v);
if (v && x > -1) {
let t = [...z.slice(0, l), ...z.slice(l + 1, len)];
z = [...t.slice(0, i), v, ...t.slice(i, len)];
}
}
return z;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment