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.

AJH
  • 175
  • 1
  • 11
  • 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. – matthew-e-brown Jun 15 '20 at 11:49
  • If you need ordered list, use arrays instead.... `ITEMS: [{type: "dress", color: "red", size: "S"}, {...}, {...}]` – OPTIMUS PRIME Jun 15 '20 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. :-) – T.J. Crowder Jun 15 '20 at 11:51
  • I would like to display them in a list but alphabetically ordered. – AJH Jun 15 '20 at 11:53
  • What exactly do you mean by `sort alphabetically dress, boots, bag`? Can you share a sample output that you are expecting after the sort? – Nithish Jun 15 '20 at 11:53
  • When rendered would love to have them as:
    • bag
    • boots
    • dress
    – AJH Jun 15 '20 at 11:55
  • 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"}}`). – T.J. Crowder Jun 15 '20 at 12:00
  • Thanks. Will use that approach instead! ^^ – AJH Jun 15 '20 at 12:17

0 Answers0