0

I am trying to figure out how to sort the objects inside a list of objects. It looks like this:

  {
    ITEMS: {
        dress: {color: "red", size: "S"}
        boots: {color: "black", size: "36"}
        bag: {color: "cream", size: "one"}
      }
    ITEMS_2: {
        dress: {color: "red", size: "S"}
        boots: {color: "black", size: "36"}
        bag: {color: "cream", size: "one"}
      }
   }

How would I sort alphabetically dress, boots, bag ?

Thanks in advance.

8
  • 1
    Since this is an object, there is no way to 'sort' it. They're indexed by their keys, not in any particular order. What are you trying to do that requires them to be sorted? This may be a slight XY problem. Commented Jun 15, 2020 at 11:49
  • If you need ordered list, use arrays instead.... ITEMS: [{type: "dress", color: "red", size: "S"}, {...}, {...}] Commented Jun 15, 2020 at 11:51
  • 1
    @matthew-e-brown - Object properties have order now. But it's still usually a bad idea to make use of that order. :-) Commented Jun 15, 2020 at 11:51
  • I would like to display them in a list but alphabetically ordered. Commented Jun 15, 2020 at 11:53
  • 1
    @AJH - I suggest using an array of {name: "dress", color: "red", "size" "S"} and similar objects (or if color and size really need to be on their own, {name: "dress", options: {color: "red", "size" "S"}}). Commented Jun 15, 2020 at 12:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.