0
var originalArray = [
    {
        name: 'Store1',
        inventory: [
            { name: 'Oranges', qt: [{ id: "something", time: 11 }, { id: "something", time: 44 }, { id: "something", time: 53 }] },
            { name: 'Mango', qt: [{ id: "something", time: 3 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Kiwi', qt: [{ id: "something", time: 2 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Papaya', qt: [{ id: "something", time: 8 }, { id: "something", time: 91 }, { id: "something", time: 3 }] }
        ]
    },
    {
        name: 'Store2',
        inventory: [
            { name: 'Pizza', qt: [{ id: "something", time: 31 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'Burger', qt: [{ id: "something", time: 1 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'IceCream', qt: [{ id: "something", time: 111 }, { id: "something", time: 11 }, { id: "something", time: 323 }] }
        ]
    }
];

For example in the above array, inventory and qt are nested keys with an array of objects as their value.
Store2 has { id: "something", time: 323 } which is a higher time than any item in Store1
After sort, Store2 will be first in the list and Store1 will be in second position, and so on, time descending

My code is not working to sort the stores.
It should not sort the nested array in inventory & qt, just sort the Store order by time descending

const sorted = originalArray
  .map(store => store.inventory
  .map(inv => inv.qt
  .map(item => Object.entries(item)[1])))
  .sort((a, b) => b[1].time - a[1].time)
  .map(item => item[1])

console.log(JSON.stringify(sorted));

EXPECTED OUTPUT

// sorted array
[
    {
        name: 'Store2',
        inventory: [
            { name: 'Pizza', qt: [{ id: "something", time: 31 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'Burger', qt: [{ id: "something", time: 1 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'IceCream', qt: [{ id: "something", time: 111 }, { id: "something", time: 11 }, { id: "something", time: 323 }] }
        ]
    },
    {
        name: 'Store1',
        inventory: [
            { name: 'Oranges', qt: [{ id: "something", time: 11 }, { id: "something", time: 44 }, { id: "something", time: 53 }] },
            { name: 'Mango', qt: [{ id: "something", time: 3 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Kiwi', qt: [{ id: "something", time: 2 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Papaya', qt: [{ id: "something", time: 8 }, { id: "something", time: 91 }, { id: "something", time: 3 }] }
        ]
    }
]
7
  • What exactly is the expected outcome for sorted? Notice that after the map, you have an array of arrays of arrays of arrays. Commented Jul 9, 2021 at 2:08
  • Are you sure you meant Object.entries(item)[1] instead of simply item.time? Commented Jul 9, 2021 at 2:10
  • @Bergi hi I've just added the expected output for the sorted array Commented Jul 9, 2021 at 2:11
  • "sort the Store order by time descending" - but by which time? Your first store has 12 times, your second store has 9 times. How do you want to choose the ones to compare? Commented Jul 9, 2021 at 2:12
  • each array will not have an equal number of objects, it has to choose all the times and get the maximum / highest time for each store & compare between stores Commented Jul 9, 2021 at 2:14

2 Answers 2

1

This should work:

const highestTime = (inventory) => {
  let max = 0;
  for (const item of inventory) {
    for (const obj of item.qt) {
      if (obj.time > max) max = obj.time;
    }
  }
  return max;
};

const sortedArray = originalArray.sort((a, b) => highestTime(b.inventory) - highestTime(a.inventory));
Sign up to request clarification or add additional context in comments.

1 Comment

thanks i further used return Math.max(..s.inventory.map(i => i.qt.map(j => j.time)).flat(Infinity)). more concise
1

This isn't the most efficient way, but it does the job

var originalArray = [
    {
        name: 'Store1',
        inventory: [
            { name: 'Oranges', qt: [{ id: "something", time: 11 }, { id: "something", time: 44 }, { id: "something", time: 53 }] },
            { name: 'Mango', qt: [{ id: "something", time: 3 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Kiwi', qt: [{ id: "something", time: 2 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Papaya', qt: [{ id: "something", time: 8 }, { id: "something", time: 91 }, { id: "something", time: 3 }] }
        ]
    },
    {
        name: 'Store2',
        inventory: [
            { name: 'Pizza', qt: [{ id: "something", time: 31 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'Burger', qt: [{ id: "something", time: 1 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'IceCream', qt: [{ id: "something", time: 111 }, { id: "something", time: 11 }, { id: "something", time: 323 }] }
        ]
    }
];
const sorted = originalArray.slice().sort((a, b) => {
    maxA = Math.max(...a.inventory.flatMap(({qt})=>qt.map(({time})=>time)));
    maxB = Math.max(...b.inventory.flatMap(({qt})=>qt.map(({time})=>time)));
    return maxB-maxA
});
console.log(sorted);

Comments

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.