-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"];
4
  • 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. Commented Jul 12, 2018 at 18:56
  • 1
    are you sure about the result? Commented Jul 12, 2018 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 :) Commented Jul 12, 2018 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; . Commented Jul 13, 2018 at 20:34

3 Answers 3

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);

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for,Thanks!
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());

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.