How can I sort an array of arrays based on another array?
Example:
const dataset = [
[ 'red', [{name: 'Apple car'}] ],
[ 'blue', [{name: 'Toothpiece'}] ],
[ 'orange', [{name: 'Sun'}] ],
[ 'yellow', [{name: 'Cat'}] ],
]
const order = ['blue', 'yellow', 'red', 'orange']
const sortedDataset = sortArrayBasedOnAnother(dataset, order)
// [
// [ 'blue', [{name: 'Toothpiece'}] ],
// [ 'yellow', [{name: 'Cat'}] ],
// [ 'red', [{name: 'Apple car'}] ],
// [ 'orange', [{name: 'Sun'}] ],
// ]
This is what I tried but it doesn't work:
function sortArrayBasedOnAnother(dataset, order) {
return order.filter((value) => dataset[0].includes(value))
}
Note: dataset is an array of arrays because it's computed from groupBy of Lodash