0

I have the following array

var dic = [
  {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, 
                              {created: 'Wed Feb 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
]

My goal is to sort by date all notifications - the finale result should be like the following - "Bob" should be the first since he got the higher date (Feb 15)

var dic = [
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, 
                              {created: 'Wed Feb 15 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, 
                              {created: 'Wed Feb 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},

]

I've tried to sort it like in the example - but i didn't manage to succeed.

var dic = [
  {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, {created: 'Wed Feb 15 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 
]

dic.sort((a,b) => {
 //console.log('a', a, 'b', b);
  if(a.notifications.length > 1){
    const test = a.notifications.reduce((a,b) => new Date(a.created).getTime() >= new Date(b.created).getTime() ? a.created : b.created);
    return new Date(test).getTime() >= new Date(b.notifications[0].created).getTime() ? -1 : 1;

  } else{
    return new Date(a.notifications[0].created).getTime() <= new Date(b.notifications[0].created).getTime() ? -1 : 1;

  }

})

console.log(dic)

3
  • 1
    Why are there two Bobs in the expected result? Commented Feb 1, 2023 at 13:45
  • As mentioned above, your input/expected output don't seem to match up. Is there a copy-paste error in there somewhere? Commented Feb 1, 2023 at 13:49
  • Can't you store these dates in some more convenient format? Commented Feb 1, 2023 at 13:56

2 Answers 2

1

This seems to work as you want it to:

var dic = [
  {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, {created: 'Wed Feb 15 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 {user:'no notifications', notifications:[]}
]

function getMaxDate(n){
  return n.notifications.reduce( (max, d) => (d = new Date(d.created).getTime(), !max || d > max ? d : max), null) ?? Date.now()
}

dic.sort((a,b) => {
  return getMaxDate(b) - getMaxDate(a)

})

console.log(dic)

Just get the date from each object, no need for an if.

Sign up to request clarification or add additional context in comments.

Comments

0

var dic = [
  {user:'John', notifications:[{created: 'Wed Jan 12 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Bob', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}, {created: 'Wed Feb 15 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
  {user:'Ron', notifications:[{created: 'Wed Jan 01 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 {user:'xxx', notifications:[{created: 'Wed Jan 31 2023 11:58:24 GMT+0200 (Israel Standard Time)'}]},
 
]

dic.sort((a,b) => {
let aSmall = a.notifications.sort((aa,bb) => { return new Date(aa.created) - new Date(bb.created) })[0];
let bSmall = b.notifications.sort((aa,bb) => { return new Date(aa.created) - new Date(bb.created) })[0];

// console.log(new Date(aSmall.created) - new Date(bSmall.created))
return new Date(aSmall.created) - new Date(bSmall.created)
})

console.log(dic)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.