I have an array of objects titled "Inventory". Each inventory object has an order property which I am sorting numerically in ascending order. Within each inventory object is an array of vehicles. And within this array exists a model property which is also an 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'
}
]
}
]
}
]
I am trying to write a method that keeps the overall sorting of the inventory array based on 'order' but sorts the 'vehicles' array based on the alphabetical order of the 'title' property within the model array.
So far I have this method which only sorts the order of the 'inventory' objects. I'm not sure if I can somehow chain an addition sort method which then sorts the vehicles array.
const sortedInventory = inventory.sort((a, b) => {
if (a.order < b.order) return -1;
if (a.order > b.order) return 1;
return 0;
})
inventory.vehicles.sort(someFunction), it's exactly the same and that will have no effect on the outer array.vehiclesarrays are completely independent of the outer array; you'll have to iterate through eachinventoryobject and sort thevehiclesarrays one by one; you can't sort all of them at the same time. It may be that I'm not understanding what you want to do.