Skip to content

Instantly share code, notes, and snippets.

@nitely
Last active March 9, 2017 00:22
Show Gist options
  • Save nitely/1daf65bb82c9207ce8a6d5c38cda6bec to your computer and use it in GitHub Desktop.
Save nitely/1daf65bb82c9207ce8a6d5c38cda6bec to your computer and use it in GitHub Desktop.
Lame JS puzzle #1
/*
We have an array of objects A and an array of indexes B. Reorder objects in array A with given indexes in array B. Do not change array A's length.
example:
var A = [C, D, E, F, G];
var B = [3, 0, 4, 1, 2];
sort(A, B);
// A is now [D, F, G, C, E];
*/
// TC O(n)
// SC O(n)
function sort(a, b) {
for (let [i, v] of [...a].entries())
a[b[i]] = v;
}
let A = ['C', 'D', 'E', 'F', 'G'];
let B = [3,0,4,1,2];
sort(A, B);
console.log(A);
// A is now [D, F, G, C, E];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment