-5

I have the following object:

   var obj = {
  "2019-01-28 00:00:00.000": [],
  "2019-01-29 00:00:00.000": [],
  "2019-01-30 00:00:00.000": [],
  "2019-02-02 00:00:00.000": []
}

How sort this object by key/date?

I tried iterate all element and convert to new Date(element) then compare with prev value.

OPV
  • 1
  • 16
  • 57
  • 124
  • 4
    Order of keys in objects is not guaranteed in JavaScript [Does JavaScript Guarantee Object Property Order?](https://stackoverflow.com/questions/5525795) – adiga Feb 18 '19 at 14:01
  • Did you try [this](https://stackoverflow.com/a/17685499/9368703)? – Prasanth Ganesan Feb 18 '19 at 14:03
  • This works when you know name of property, my case is different – OPV Feb 18 '19 at 14:05
  • You would have to split this object into a sorted array of objects for it to work properly. – Adrian Feb 18 '19 at 14:07
  • [This](https://stackoverflow.com/a/14528472/9368703) will work then – Prasanth Ganesan Feb 18 '19 at 14:09
  • You don’t need to convert into Date instances here, these values are sortable using simple string comparison already. – 04FS Feb 18 '19 at 14:11
  • @adiga actually, it's been a while the order is guaranteed: http://2ality.com/2015/10/property-traversal-order-es6.html#traversing-the-own-keys-of-an-object and you can rely on that (but I would strongly suggest to use `Map` if the order is mandatory). – ZER0 Feb 18 '19 at 14:24
  • @adiga as said, better use `Map` IMVHO, however, the order is predictable and the OP doesn't actually deal with "numerical keys" – they're string, and "a String property name `P` is an array index if and only if `ToString(ToUint32(P))` is equal to `P` and `ToUint32(P)` is not equal to `2^32-1`", and definitely this is not the case of the OP. – ZER0 Feb 18 '19 at 14:43
  • @ZER0 In that case [the mdn documents](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Deleted_added_or_modified_properties) need to be updated. – HMR Feb 18 '19 at 14:43
  • @HMR definitely! Although the `for…in` and `Reflect.enumerate` is different from `Object.keys`, `Object.getOwnPropertyNames` and `Reflect.ownKeys`; the order is still defined. – ZER0 Feb 18 '19 at 14:46
  • @ZER0 yes, I should've read [this section](http://2ality.com/2015/10/property-traversal-order-es6.html#integer-indices) more carefully. – adiga Feb 18 '19 at 14:49

2 Answers2

2

Order of keys is not fixed in javascript. You could extract the keys, sort the keys and then run a function for each key

var objKeys = Object.keys(obj);
objKeys.sort();
objKeys.forEach( (value) => {
    // do something with obj[value]
});
Cosmin Staicu
  • 1,514
  • 2
  • 18
  • 21
1

You can use this:

var keys= Object.keys(obj).sort(function(a,b){
    // Probably you cast the Date applying a new Date()
    // Also you can use > to order DESC
    // You can create a function to order ASC or DESC
    return a<b;
});

// Now you could iterate your object ordered
keys.foreach(function(k) {
    // This your fist element ordered obj[k]
});

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

roag92
  • 146
  • 1
  • 6