-1

I have an object in javascript that looks like this:

const inventory = {
   fall: { 'Category 1': [], 'Category 2': [] },
   spring: { 'Category 1': [], 'Category 3': [] },
   winter: { 'Category 3': [], 'Category 4': [] },
   summer: { 'Category 4': [], 'Category 5': [] }
}

How can I sort by keys so that those are ordered as follows:

  1. spring
  2. summer
  3. fall
  4. winter
zoltalar
  • 119
  • 1
  • 9
  • 5
    Why do you need to sort them? You have 4 well defined properties that you can access by key. Are you iterating over them for some reason? – Chase Aug 30 '22 at 13:37
  • 1
    in JS you cannot sort by key like you can in PHP. You can have a look at some other similar questions with answers on SO: https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key – Cornel Raiu Aug 30 '22 at 13:39
  • Yes, I'm iterating over them and sometimes they produce different output like spring, summer, winter, fall OR winter, fall, summer, spring. I want it to be consistent – zoltalar Aug 30 '22 at 13:41
  • 1
    Although object keys are iterated in a certain order, the rules for that can be a pitfall. Most agree that it is bad practice to rely on any order of object keys. If you need order, you should be using an array. Like: `[{season: "spring", category1: [], category2: []}, {season: "summer", .........]` – trincot Aug 30 '22 at 13:41
  • You can look at [this](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order/38218582#38218582) and [this](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order). but it's best not to rely on the default object ordering in js. If you want ordered pairs, use Maps – nullptr Aug 30 '22 at 13:42
  • You could use Sets, they preserve the order: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set – kissu Aug 30 '22 at 13:48

2 Answers2

0

Destructuring assignment

const { spring, summer, fall, winter } = {
  fall: { 'Category 1': [], 'Category 2': [] },
  spring: { 'Category 1': [], 'Category 3': [] },
  winter: { 'Category 3': [], 'Category 4': [] },
  summer: { 'Category 4': [], 'Category 5': [] }
};

const inventory = { spring, summer, fall, winter }
php-dev
  • 6,998
  • 4
  • 24
  • 38
  • 3
    This will create an object with the properties in the desired order, but JavaScript code that expects a particular ordering is a bad practice. – Pointy Aug 30 '22 at 13:44
  • @Pointy I'm not sure about the question purpose – php-dev Aug 30 '22 at 13:46
0

You just need to create new object with this sorting.

const inventory = {
   fall: { 'Category 1': [], 'Category 2': [] },
   spring: { 'Category 1': [], 'Category 3': [] },
   winter: { 'Category 3': [], 'Category 4': [] },
   summer: { 'Category 4': [], 'Category 5': [] }
}

const { fall, spring, winter, summer } = inventory;

const sortingInventory = {
  spring,
  summer,
  fall,
  winter,
}


console.log(sortingInventory)

FOR SORTING ALGHPITICALLY.

const inventory = {
   fall: { 'Category 1': [], 'Category 2': [] },
   spring: { 'Category 1': [], 'Category 3': [] },
   winter: { 'Category 3': [], 'Category 4': [] },
   summer: { 'Category 4': [], 'Category 5': [] }
}

const result = Object.fromEntries(Object.entries(inventory).sort((a, b) => {
  return a[0].localeCompare(b[0])
}))

console.log(result)
Mina
  • 14,386
  • 3
  • 13
  • 26