I am trying to sort an array of key-value paired objects using the index of a second array of arrays.
Consider the following code:
const sheet = [['ES'], ['MES'], ['NQ'], ['MNQ']] // sort in this order
const quotes = [{root: 'MNQ'}, {root: 'MES'}, {root: 'ES'}, {root: 'NQ'}] // sort these objects
const sorted = quotes.sort((a, b) => {
return sheet.indexOf(a.root) - sheet.indexOf(b.root)
})
console.log(sorted)
When you run the above code, sorted has retained the same order as the original quotes variable and it appears as if no sort has taken place!
I feel like I'm almost there and my code just needs a small tweak to make it work.