Reorder an array in place by a list of new indexes

easyAbout 20 minutesUpdated CodingProarraysin-placepermutationscycles
Not solved

Share this question

Problem

A drag-and-drop list sends back where each item ended up: newOrder[i] is the new index of the item that was at index i. Write reorder(items, newOrder) that moves every item to its new index in place and returns items.

const items = ["A", "B", "C", "D", "E", "F"];
reorder(items, [1, 5, 4, 3, 2, 0]);
// "A" moves to 1, "B" to 5, "C" to 4, "D" stays at 3, "E" to 2, "F" to 0
items; // ["F", "A", "E", "D", "C", "B"]

Ref. UR-1418 · © UIRound · uiround.com/questions/reorder-an-array-in-place-by-a-list-of-new-indexes

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment