Typescript - How to sort one object array based on another array with sorted object's fields?
For example:
Array A not sorted
Array B with object's field sorted
Tries to sort Array A based on Array B's order.
const unsortedArray = [
{
repId: "4",
symbol: "MSLA",
orderNo: "20180518-00004"
},
{
repId: "2",
symbol: "TSLA",
orderNo: "20180518-00003"
},
{
repId: "55",
symbol: "APPL",
orderNo: "20180518-00001"
},
{
repId: "22",
symbol: "FB",
orderNo: "20180518-0002"
}]
const sortedArrayField = [
"20180518-00001",
"20180518-00002",
"20180518-00003",
"20180518-00004"
]
// This is the sorted order that I want
const sortedArray = [
{
repId: "55",
symbol: "APPL",
orderNo: "20180518-00001"
},
{
repId: "22",
symbol: "FB",
orderNo: "20180518-00002"
},
{
repId: "2",
symbol: "TSLA",
orderNo: "20180518-00003"
},
{
repId: "4",
symbol: "MSLA",
orderNo: "20180518-00004"
}]
The sorted field array can be anything, not just order number, could be symbol, status, etc... Any suggestion???
Looked up some example and tried a few approach, but no elegant solution so far with typescript. No external library plz.