0

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;
      })
5
  • inventory.vehicles.sort(someFunction), it's exactly the same and that will have no effect on the outer array. Commented Jul 23, 2019 at 15:25
  • Do I chain another sort method and drill down the the appropriate array? An example would help. Commented Jul 23, 2019 at 15:26
  • The vehicles arrays are completely independent of the outer array; you'll have to iterate through each inventory object and sort the vehicles arrays 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. Commented Jul 23, 2019 at 15:33
  • Yes this is what I am trying to accomplish Commented Jul 23, 2019 at 17:21
  • Can you add how expected result should look like. Commented Jul 23, 2019 at 17:28

1 Answer 1

1

You can loop through your inventory list and sort vehicles for each inventory without affecting the initial order of the inventories;

Update Answer editted per OP's comment.

for (const inv of inventory) {
     inv.vehicles.sort((a, b) => { 
             if (a.model[0].title < b.model[0].title) return 1;
             if (a.model[0].title > b.model[0].title) return -1;
             return 0;
         });
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm actually not trying to sort the models array. I'm trying to sort the vehicles array based on the alphabetical order of the title. IE vehicle[i].model[0].title.. With my data, there's always only one object in the model array. However there can be multiple objects in the vehicle array.
getting '[object Object] is not iterable' w/ this solution
@LDB your JSON seems to be faulty. For instance, an array opens at line 16 which is not a property of the inventory object. Make sure inventory is a proper list of inventory objects and the code above should work just fine
Thx. I meant to edit my last comment. This solution works and I have accepted the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.