我有一个要按两个属性排序的对象数组:

  • 提醒时间戳
  • ModificationTimestamp

  • 排序顺序:desc

    按一个属性对这个对象进行排序不是问题,但是在这种情况下,我不知道如何使其工作。

    最佳答案

    假设时间戳本身可以排序(例如ISO8601和相同的时区),请尝试:

    myArray.sort(function(a,b) {
      var x = a.RemindingTimestamp - b.RemindingTimestamp;
      return x == 0? a.ModificationTimestamp - b.ModificationTimestamp : x;
    }
    

    编辑-评论回应

    通过更改减法顺序或将结果乘以-1可实现降序排序。由于未减去日期而无法排序的日期(例如2012-04-12)可以通过首先转换为日期来处理,例如
    // Convert ISO8601 date string to date object
    // Assuming date is ISO8601 long format, ignores timezone
    function toDate(s) {
      var bits = s.split(/[-T :]/);
      var d = new Date(bits[0], bits[1]-1, bits[2]);
      d.setHours(bits[3], bits[4], parseFloat(bits[5]));
      return d;
    }
    
    // Source data, should end up sorted per n
    var myArray = [
      {RemindingTimestamp: '2012-04-15T23:15:12Z',
       ModificationTimestamp: '2012-04-15T23:15:12Z', n: 4},
      {RemindingTimestamp: '2012-04-12T23:15:12Z',
       ModificationTimestamp: '2012-04-12T23:15:12Z', n: 1},
      {RemindingTimestamp: '2012-04-12T23:15:12Z',
       ModificationTimestamp: '2012-04-13T23:15:12Z', n: 2},
      {RemindingTimestamp: '2012-04-12T23:15:12Z',
       ModificationTimestamp: '2012-04-13T23:15:14Z', n: 3}
    ];
    
    // Sort it
    myArray.sort(function(a,b) {
      var x = toDate(a.RemindingTimestamp) - toDate(b.RemindingTimestamp);
      return x? x : toDate(a.ModificationTimestamp) - toDate(b.ModificationTimestamp);
    });
    
    // Just to show the result
    function sa(o) {
      var result = [], t;
      for (var i=0; i<o.length; i++) {
        t = o[i];
          result.push(t.n);
      }
      alert(result);
    }
    
    sa(myArray); // 1,2,3,4
    

    如果需要,可以将日期字符串转换为日期对象,以处理时区(仅对于符合ISO8601的字符串,使用时区缩写而不是实际偏移量的字符串不可靠)。

    09-04 16:34
    查看更多