0

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

Tomtetot
  • 5
  • 4

2 Answers2

0

I find the following more readable. Get the keys from the object(a.k.a the dates), map each entry in the object to a new 'key', 'value' pair object and then sort by the date property.

var dates = { '10/25/2019': 'sth',
         '6/21/2017': 'sthelse',
         '4/15/2016': 'anotherone',
         '12/29/2015': 'wtf',
         '8/18/2018': 'lol' };
var sorted = Object.keys(dates)
                   .map(k => ({ key: new Date(k), value: dates[k] }))
                   .sort((a,b) => a.key-b.key)
                   .reduce((cv,pv) => {
                       cv[pv.key] = pv.value;
                       return cv;
                   }, {});
console.log(sorted);
peinearydevelopment
  • 11,042
  • 5
  • 48
  • 76
  • Now it does return an object, but it modifies the keys ... – derpirscher Jun 22 '21 at 20:58
  • That's a more structured way yes, although I'm not perfectly familiar with map and reduce yet. I'll try around a bit and see what suits me best. Thanks for your answer and help! – Tomtetot Jun 22 '21 at 22:13
0

While I don't really understand WHY the order of properties in an object would be of any relevance (and you shouldn't rely on it) this should work

function sort(o) {
  let keys = Object.keys(o)
    .sort((a,b) => new Date(a).getTime() - new Date(b).getTime());

  let r = {};
  for (let k of keys) r[k] = o[k];
  return r;
}

And if you like reduce you can do it in a single statement

function sort(o) {
  return Object.keys(o)
    .sort((a,b) => new Date(a).getTime() - new Date(b).getTime()) 
    .reduce((a, k) => { 
        a[k] = o[k];
        return a;
      }, {});
}

But IMHO the loop is more readable, and a don't really see an advantage in using reduce here ...

derpirscher
  • 14,418
  • 3
  • 18
  • 35
  • That's what I was looking for! The WHY is probably just because my coding skills are still 0 :D Thanks for your answer and help! Best regards – Tomtetot Jun 22 '21 at 22:21