I am new to coding and trying to learn, so sorry if this question might be doubleposted. I searched the forum and found some hints but not a solution.
I have an object which looks like this:
Dates: { '10/25/2019': 'sth',
         '6/21/2017': 'sthelse',
         '4/15/2016': 'anotherone',
         '12/29/2015': 'wtf',
         '8/18/2018': 'lol' }
I want it to be sorted by the date, latest date to the oldest date. The date key however is a string and in the end result it should be the same. I first tried to split the key with '/' and then create a score with priority1 being the year, priority2 the month, prio3 the day but it ended up in spaghetti and I couldn't figure it out.
So I found following in the forum: How to sort an object array by date property? and tried to figure it out but unfortunately failed as I am not sorting an array of objects, I'm sorting an object itself.
Here is my solution
function sortthedates(unsorted){
  sorteddates = {}
  datesunsorted = unsorted;
  key = Object.keys(datesunsorted);
  value = Object.values(datesunsorted);
  var sorted = key.sort((a, b) => {
    return new Date(b) - new Date(a);
  });
  for (var y = 0; y < sorted.length; y++){
    sorteddates[sorted[y]] = datesunsorted[sorted[y]];
  }
  return sorteddates;
}
which produces the correct result, but seems to be very spaghetti code like and unefficient.
Is there a better easier and shorter way to do the task?
Thanks in advance for help :) Best regards
 
     
    