-1

I have an array and I’m trying to re-order this array based on another array. The second array is an array of indexes (see below). I’m looking to build a clean function to take two arguments (an array and an array of indexes), and return the re-ordered array. I've tried to build this function and have an example below, however it's not returning what I'm expecting. Any help is greatly appreciated.

var before = ["T", "T", "A", "T", "T", "T", "T", "T", "A", "T", "T","T", "W", "W", "W", "W", "T", "T", "T", "T", "T", "W", "T"];
var indexes = [8, 2, 11, 22, 0, 4, 5, 18, 6, 17, 16, 19, 7, 3, 20, 1, 10, 9, 14, 13, 21, 12, 15];

// Attempt
function reorderArray(arrayToOrder ,order){
    // Get a copy of the array we want to change
    var temp = arrayToOrder
    // loop through the indexes
    // use the indexes to place the items in the right place from the copy into the original
    for(let i = 0; i < arrayToOrder.length; i++) {
        console.log("arr: ", arrayToOrder[order[i]] );
        console.log("temp: ", temp[i] );
        arrayToOrder[order[i]] = temp[i];
    }
    return arrayToOrder;
}
// run function
reorderArray( before, indexes );

// function should return this array
var after = ["A", "A", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "W", "W", "W", "W", "W"];
MostlyRquestions
  • 526
  • 2
  • 7
  • 22
  • Please note that copying reference of an object (arrays are also objects) doesn't create a copy. It actually create another reference to the same object. – Mohammad Usman Jul 12 '18 at 18:56
  • 1
    are you sure about the result? – Nina Scholz Jul 12 '18 at 19:10
  • Your `after` looks like an alpha SORT not an index of the other array list. Is this supposed to be a trick question :) – Mark Schultheiss Jul 12 '18 at 19:15
  • The question needs to be retitled, reworded, or corrected, The example code show how to reorder an array according to "rank", since it uses something like `arrayToOrder[rank[i]] = temp[i];` . If reordering an array according to "indexes", the key line of code is `arrayToOrder[i] = temp[index[i]];` . I don't know which case is more common, but one example of this is sorting an array of indexes 0 to length-1, according to another array, and in this case, the reorder is done according to indexes. You can convert indexes to rank : `rank[index[i]] = i;` or vice versa `index[rank[i]] = i;` . – rcgldr Jul 13 '18 at 20:34

3 Answers3

3

You can use Array.prototype.map

var before = ["T", "T", "A", "T", "T", "T", "T", "T", "A", "T", "T","T", "W", "W", "W", "W", "T", "T", "T", "T", "T", "W", "T"];
var indexes = [8, 2, 11, 22, 0, 4, 5, 18, 6, 17, 16, 19, 7, 3, 20, 1, 10, 9, 14, 13, 21, 12, 15];

var output = indexes.map(i => before[i]);

console.log(output);
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
1

Iterate the indexes with Array.map(), and return the value from the before array:

const before = ["T", "T", "A", "T", "T", "T", "T", "T", "A", "T", "T","T", "W", "W", "W", "W", "T", "T", "T", "T", "T", "W", "T"];
const indexes = [8, 2, 11, 22, 0, 4, 5, 18, 6, 17, 16, 19, 7, 3, 20, 1, 10, 9, 14, 13, 21, 12, 15];

const reorderByIndexes = (arr, order) => order.map((index) => arr[index]);
  
const after = reorderByIndexes(before, indexes);

console.log(after.join());
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
0

Or with a forEach if you don't want to use ES6

var before = ["T", "T", "A", "T", "T", "T", "T", "T", "A", "T", "T", "T", "W", "W", "W", "W", "T", "T", "T", "T", "T", "W", "T"];
var indexes = [8, 2, 11, 22, 0, 4, 5, 18, 6, 17, 16, 19, 7, 3, 20, 1, 10, 9, 14, 13, 21, 12, 15];
var after = [];
indexes.forEach(function(value, index) {
  after[index] = before[value]
})

console.log(after)
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
IsraGab
  • 4,819
  • 3
  • 27
  • 46