Many similar questions but none seem to really apply to this.
I am trying to sort the order of arrays in ArrayA based on the elements in ArrayB in JavaScript
For example, ArrayA and ArrayB are as follows.
ArrayA = [
['1', '123', '321'],
['2', '456', '654'],
['3', '789', '987'],
['4', '420', '314']
]
ArrayB = ['2', '1', '4', '3'];
I basically want to map the order of the arrays inside ArrayA to match the order of the elements in ArrayB. I want to be able to do this with any sized arrays not just 4... (assuming they are the same size)
The desired output is below:
ArrayA = [
['2', '456', '654'],
['1', '123', '321'],
['4', '420', '314'],
['3', '789', '987']
]
I've tried many .sort() solutions and manual loops and nothing seems to work (such as the one below and yes I know its wrong). So just wondering if anyone has any ideas. TIA!
ArrayA.sort((a, b) => ArrayB.indexOf(a.ArrayA) - ArrayB.indexOf(b.ArrayA));