Looking to implement sorting functionality for the "fuel.name" value within this array...
const orders = [
{
"_id":"5d14a31490fb1e0012a3d2d8-1",
"orderId":"0JL5ORM0JT-1",
"created":"2019-06-27T11:05:56.377Z",
"createdDate":"2019-06-27T09:05:56.377Z",
"offers":[
{
"price":95.27,
"fuel":{
"_id":"5ce13948eaef5200113b0de8",
"name":"Diesel B7",
"description":"Diesel",
"lpt":0,
"duty":0,
"type":"SPOT",
"created":"2019-05-19T11:08:56.417Z"
}
},
{
"price": 95.27,
"fuel": {
"_id": "5ce13948eaef5200113b0de8",
"name": "Petrol",
"description": "Petrol",
"lpt": 0,
"duty": 0,
"type": "SPOT",
"created": "2019-05-19T11:08:56.417Z"
}
},
{
"price": 95.27,
"fuel": {
"_id": "5ce13948eaef5200113b0de8",
"name": "Fossil Fuel",
"description": "Fossil Fuel",
"lpt": 0,
"duty": 0,
"type": "SPOT",
"created": "2019-05-19T11:08:56.417Z"
},
}
]
}
]
The goal is to rearrange the "offers" objects based on the "fuel.name"
orders.sort((a: any, b: any) => a.offers[0].fuel.name.toUpperCase().localCompare(b.offers[0].fuel.name))
After console logging the above code, it's not returning the desired order. To replicate the issue, you can check out this fiddle.