There is a function where I can sort maybe prices from highest to low or from low to high. Here is an example of the function
myarray.sort((a,b) => b.age - a.age)
But how can I sort when I have some objects in my array and in there is an array of objects and this prices I have to sort.
Here in durationcontract is the array and there are prices
Array:
const arr = [
{
id: 25,
name: 'Mercedes-Benz C63s AMG',
durationcontract: [
{
duration: '12',
price: '24'
},
{
duration: '24',
price: '800'
},
],
},
{
id: 25,
name: 'Mercedes-Benz C63s AMG',
durationcontract: [
{
duration: '12',
price: '60'
},
{
duration: '24',
price: '120'
},
],
]
I have to sort all items in the array by the highest or lowest price in durationcontract.
I am struggling and hope anyone can help me
expected Output:
[
{
id: 12,
name: 'Seat',
durationcontract: [
{
duration: '12',
price: '24'
},
{
duration: '24',
price: '1200'
},
],
},
{
id: 25,
name: 'Mercedes-Benz C63s AMG',
durationcontract: [
{
duration: '12',
price: '24'
},
{
duration: '24',
price: '800'
},
],
},
{
id: 25,
name: 'Mercedes-Benz C63s AMG',
durationcontract: [
{
duration: '12',
price: '60'
},
{
duration: '24',
price: '120'
},
],
]