0

I have this object in my project. The keys within this object are dates. I'm trying to sort by chronological order. So for example:

//unordered
var mod = { 
   01-01-2019: { },
   03-03-2018: { },
   12-01-2017: { }
}
//ordered
mod = {
   12-01-2017: { },
   03-03-2018: { },
   01-01-2019: { }
}

I've tried .sort() but it's sorting by the month first. Any help would be appreciated.

*EDIT - I attempted to use the sort() routine, but with no luck of ordering it chronologically, is there a better way of sorting by date with the sort functionality?

dlimes
  • 83
  • 10
  • 2
    Properties don't have order – Sebastian Speitel Dec 11 '18 at 18:37
  • sort accepts a custom function that you can use - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Velimir Tchatchevsky Dec 11 '18 at 18:39
  • 5
    @SebastianSpeitel Yes they do, **properties *are* ordered, in modern JavaScript**. They are ordered by their order of creation. It's damaging to continue to spread that misinformation. – meager Dec 11 '18 at 18:40
  • @Amy I used that solution, but it wasn't ordering it based off of chronological order, it was basing it off of the first characters. – dlimes Dec 11 '18 at 18:41
  • @dlimes Then change your sort function accordingly. –  Dec 11 '18 at 18:41
  • @meagar yes they keep there order, but it's not good practice to depend on it – Sebastian Speitel Dec 11 '18 at 18:42
  • @Amy - edited my question.... I'm trying to sort it still, but would like to know how to sort chronologically. – dlimes Dec 11 '18 at 18:48
  • @dlimes A quick search ("site:stackoverflow.com sort date format mm-dd-yyyy") turned up https://stackoverflow.com/questions/23084782/how-sort-array-date-javascript-dd-mm-yyyy –  Dec 11 '18 at 18:52

0 Answers0