Whisher
Whisher

Reputation: 32726

*ngFor keyvalue descOrder doesn't work with date

I'm using entities

the format is like

{
  'mongooseId': {
    title: 'my title',
    created: '2018-08-31T10:04:52.673Z'
  }
}

and I'd like :) to put them

in descOrder by the created field

descOrder(a, b){
  if(a.value.created > b.value.created){
    return b.value.created;
  }
}
*ngFor="let post of posts | keyvalue:descOrder"

but it doesn't work

even if I cast che string to date

new Date(a.value.created)

UPDATE

I change the format of the date

using Date.now() so now its a number

and with this code it works :)

descOrder(a, b){
    if(b.value.created > a.value.created) {
      return a.value.created;
    }
  }

Upvotes: 1

Views: 149

Answers (1)

Krishna Rathore
Krishna Rathore

Reputation: 9687

return b.key for that

descOrder(a, b){
  if(a.value.created < b.value.created){
    return b.key;
  }
}

Upvotes: 1

Related Questions