按日期顺序对Node.js中的数组进行排序(最近到最旧)

时间:2020-02-19 22:44:07

标签: javascript arrays sorting

我正在尝试对以下数组进行排序,以使最新的event_end为第一

{
   "results":[
      {
         "event_start":"2017-11-27T09:00:00Z",
         "event_end":"2017-11-27T09:00:00Z",
         "attendance":0,
         "title":"Administrate Training Session",
         "type":"delegate"
      },
      {
         "event_start":"2018-02-01T09:00:00Z",
         "event_end":"2018-02-01T09:00:00Z",
         "attendance":0,
         "title":"Health and Safety Awareness (HSA)",
         "type":"delegate"
      },
      {
         "event_start":"2018-02-19T09:00:00Z",
         "event_end":"2018-04-30T09:00:00Z",
         "attendance":0,
         "title":"SMSTS",
         "type":"delegate"
      }
   ]
}

我当前的代码(这是在尝试了几乎所有不同方法后执行的):

Array.from(outcome).sort(sortFunction);
      function sortFunction(a, b){
        if(b[3] === a[3]){
          return 0;
        } else {
          return (b[3] < a[3]) ? -1 : 1;
        }
      }

只是为了说明如何创建数组:

var history = JSON.parse(body);
      var outcome = {};
      var key = 'results';
      outcome[key] = [];
      history.forEach(delegate => {
          var data = null;
          var sessionKey;
          var attendanceCount = 0;
          var sessionCount = 0;   
          var attended = 0;    
          Array.from(delegate['session_attendance']).forEach(function(val){
            if(!val.__proto__.__proto__){
              sessionCount++;
            }
          });      
          var type;              
          for(var k in delegate['session_attendance']){
            sessionKey = k;
            if(k['status'] == true){
              attendanceCount++;
            }
          }
          if(attendanceCount == 0){
            attended = attendanceCount;
          } else {
            (attendanceCount / sessionCount) * 100
          }
          if(delegate['registration']['booking_contact'] !== null){
            if(delegate['registration']['booking_contact']['id'] == delegate['contact']['id']){
              type = 'booking_contact';
            } 
          } else{
            type = 'delegate';
          }
          data = {
            'objectId': delegate['id'],                
            'title': delegate['event']['title'],
            'event_start': delegate['event']['start'],
            'event_end': delegate['session_attendance'][sessionKey]['start'],
            'attendance': attended,
            'type': type
          }
          outcome[key].push(data);
        })

我确定它是显而易见的,但是谁能指出我出问题的方向以及如何对其进行正确排序?

2 个答案:

答案 0 :(得分:0)

sort接收的函数有2个参数,每个参数都是一个obj,因此您可以访问其属性。

类似的事情应该起作用:

arr.sort((a, b) => {
    return a.event_end > b.event_end ? -1 : 1;
})

答案 1 :(得分:0)

var obj = {
   "results":[
      {
         "event_start":"2017-11-27T09:00:00Z",
         "event_end":"2017-11-27T09:00:00Z",
         "attendance":0,
         "title":"Administrate Training Session",
         "type":"delegate"
      },
      {
         "event_start":"2018-02-01T09:00:00Z",
         "event_end":"2018-02-01T09:00:00Z",
         "attendance":0,
         "title":"Health and Safety Awareness (HSA)",
         "type":"delegate"
      },
      {
         "event_start":"2018-02-19T09:00:00Z",
         "event_end":"2018-04-30T09:00:00Z",
         "attendance":0,
         "title":"SMSTS",
         "type":"delegate"
      }
   ]
}

obj.results.sort((a, b) => {
  return new Date(b.event_end) - new Date(a.event_end)
})

console.log(obj.results)