I am dealing with an array of objects known as "Inventory". In each inventory object, there is an order property that I need to sort in ascending numerical order. Each inventory object also contains an array of vehicles, and within this array lies a model property which is another array.
inventory: [
{
category: American,
order: 1,
vehicles: [
{
instock: 'yes',
model: [
{
lang: 'en-US'
title: 'mustang'
}
]
}
],
[
{
instock: 'no',
model: [
{
lang: 'en-US'
title: 'viper'
}
]
}
],
[
{
instock: 'yes',
model: [
{
lang: 'en-US'
title: 'camaro'
}
]
}
]
}
]
My goal is to create a method that will maintain the sorting of the inventory array based on the 'order' property while also sorting the 'vehicles' array alphabetically by the 'title' property within the model array.
I have attempted a method that successfully sorts the order of the 'inventory' objects, but I am unsure if I can incorporate an additional sorting method to arrange the vehicles array accordingly.
const sortedInventory = inventory.sort((a, b) => {
if (a.order < b.order) return -1;
if (a.order > b.order) return 1;
return 0;
})