0

When I console.log the details variable, I get the following results:

details:{ 1:{ 2015-2-10 : 0 2015-2-11 : 0 2015-2-12 : 0 2015-2-13 : 0 2015-2-14 : 0 2015-2-15 : 0 2015-2-16 : 0 2015-2-17 : 0 2015-2-18 : 0 2015-2-19 : 0 2015-2-2 : 0 2015-2-20 : 0 2015-2-21 : 0 2015-2-22 : 0 2015-2-23 : 0 2015-2-24 : 0 2015-2-25 : 0 2015-2-26 : 0 2015-2-27 : 1 2015-2-28 : 1 2015-2-3 : 0 2015-2-4 : 0 2015-2-5 : 0 2015-2-6 : 0 2015-2-7 : 0 2015-2-8 : 0 2015-2-9 : 0 2015-3-1 : 0 } }

I want to do is to display the dates with ascending order. So the output will become:

details:{ 1:{ 2015-2-2 : 0 2015-2-3 : 0 2015-2-4 : 0 2015-2-5 : 0 2015-2-6 : 0 2015-2-7 : 0 2015-2-8 : 0 2015-2-9 : 0 2015-2-10 : 0 2015-2-11 : 0 2015-2-12 : 0 2015-2-13 : 0 2015-2-14 : 0 2015-2-15 : 0 2015-2-16 : 0 2015-2-17 : 0 2015-2-18 : 0 2015-2-19 : 0 2015-2-20 : 0 2015-2-21 : 0 2015-2-22 : 0 2015-2-23 : 0 2015-2-24 : 0 2015-2-25 : 0 2015-2-26 : 0 2015-2-27 : 1 2015-2-28 : 1 2015-3-1 : 0 } }

My question: Is it possible to do it?

my plunker is http://plnkr.co/edit/dQO3D1Omc0o7ZH9TfeUZ?p=preview Thanks in advance!

QWERTY
  • 263
  • 5
  • 20
  • possible duplicate of [Sort JavaScript object by key](http://stackoverflow.com/questions/5467129/sort-javascript-object-by-key) – Oleksandr T. Mar 17 '15 at 09:00

1 Answers1

1

My question: Is it possible to do it?

No. The structure you've quoted is an object with property names like "2015-02-28". Objects in JavaScript have no order to their properties at all. To have order, you must (for now) have an array. ES6 will introduce Map objects, which are maps ordered by the insertion order of their keys. But ES5 doesn't have those.

That doesn't mean you can't access those properties in any order you want. You can get an array of the property names via Object.keys, and you can sort arrays however you like.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639