最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React sorting by date - Stack Overflow

programmeradmin1浏览0评论

I have a react component that renders list of "Meetings" that have occurred in the past.

I'm trying to get it to sort each past meeting so that those that occurred more recently in the past will be at the top.

I am checking to see if the meeting occurred in the past, if so, display it. (This is working fine)

This is my render method:

render(){
let past_meetings = this.store.meetings.map((meeting, i) => {
  if (meeting.scheduled_for < moment.utc( new Date() ).format()){
    return (<MeetingItem meeting={meeting} key={`meeting-${meeting.id}`} />)
  } else {
    return ;
  }
})

return(
  <div className="">
    <div className=""> 
      <h3 className="">Past Meetings</h3>
      <div className="" > 
        {past_meetings}
      </div>
    </div> 

  </div>
  )
 }

Currently the meetings are shown as: if it happened further in the past, show at the top.

My goal is to get it to show as follows: if it happened more recently, show at the top.

Tried something like this with lodash's .sortBy():

let sorted_meetings = _.sortBy(past_meetings, function(e){
return - (new Date(e.scheduled_for))
}

no success.

meeting.scheduled_for
// returns 2017-03-03T03:33:00.000Z

tried to compare the scheduled_for's but not much luck either.

can this be done with _ ??? or any other way?

I have a react component that renders list of "Meetings" that have occurred in the past.

I'm trying to get it to sort each past meeting so that those that occurred more recently in the past will be at the top.

I am checking to see if the meeting occurred in the past, if so, display it. (This is working fine)

This is my render method:

render(){
let past_meetings = this.store.meetings.map((meeting, i) => {
  if (meeting.scheduled_for < moment.utc( new Date() ).format()){
    return (<MeetingItem meeting={meeting} key={`meeting-${meeting.id}`} />)
  } else {
    return ;
  }
})

return(
  <div className="">
    <div className=""> 
      <h3 className="">Past Meetings</h3>
      <div className="" > 
        {past_meetings}
      </div>
    </div> 

  </div>
  )
 }

Currently the meetings are shown as: if it happened further in the past, show at the top.

My goal is to get it to show as follows: if it happened more recently, show at the top.

Tried something like this with lodash's .sortBy():

let sorted_meetings = _.sortBy(past_meetings, function(e){
return - (new Date(e.scheduled_for))
}

no success.

meeting.scheduled_for
// returns 2017-03-03T03:33:00.000Z

tried to compare the scheduled_for's but not much luck either.

can this be done with _ ??? or any other way?

Share Improve this question edited Mar 1, 2017 at 19:07 JAbrams asked Mar 1, 2017 at 19:02 JAbramsJAbrams 3254 gold badges7 silver badges19 bronze badges 3
  • 1 what version of loadash you are using? – Nicholas Commented Mar 1, 2017 at 19:08
  • You need a descending order sort. I don't know what version of Loadash you are using. You can try the options in the accepted answer. This link. – Nicholas Commented Mar 1, 2017 at 19:11
  • 1 @Nicholas ^4.17.4 – JAbrams Commented Mar 1, 2017 at 19:14
Add a comment  | 

1 Answer 1

Reset to default 23

You can use sort() and reverse() to sort meetings in descending order

var sorted_meetings = meetings.sort((a,b) => {
    return new Date(a.scheduled_for).getTime() - 
        new Date(b.scheduled_for).getTime()
}).reverse();
发布评论

评论列表(0)

  1. 暂无评论