Last active
March 25, 2016 22:52
-
-
Save ceane/217ea04fc2759ecea21d to your computer and use it in GitHub Desktop.
Simple sort
This file contains 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
/** | |
* 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