【问题标题】:How to sort array of objects by time string value?如何按时间字符串值对对象数组进行排序?
【发布时间】:2016-10-08 17:48:58
【问题描述】:

我有一个这样的对象数组:

var example = [{
  "description": "aaa",
  "time": "12:15pm"
}, {
  "description": "bbb",
  "time": "10:10am"
}, {
  "description": "ccc",
  "time": "4:00pm"
}, {
  "description": "ddd",
  "time": "6:15pm"
}, {
  "description": "eee",
  "time": "1:10am"
}, {
  "description": "fff",
  "time": "5:00pm"
} ];

我想按time 值排序。

我已尝试应用 this solution,它用于字符串值数组:

example.sort(function (a, b) {
  return new Date('1970/01/01 ' + a.time) - new Date('1970/01/01 ' + b.time);
});

console.log(example);

我也一直在参考 Mozilla Array.prototype.sort() 文档并尝试了以下似乎不起作用的方法:

example.sort(function(a, b) {
  if (new Date(a.time) > new Date(b.time)) {
    return 1;
  }
  if (new Date(a.time) < new Date(b.time)) {
    return -1;
  }
  // a must be equal to b
  return 0;
});

console.log(example);

【问题讨论】:

  • 您的问题是不是要解析的有效日期格式。
  • new Date("1:10am"); //Invalid Date
  • 我会尝试在 am/pm 后缀前加空格。

标签: javascript sorting date


【解决方案1】:

您生成的日期字符串无效,因此它将始终返回当前日期和时间。所以生成有效的日期字符串(例如:'1970/01/01 9:34:48 AM')然后解析并返回差异。这里可以使用String#slice()方法生成有效的日期字符串。

var example = [{
  "description": "aaa",
  "time": "12:15pm"
}, {
  "description": "bbb",
  "time": "10:10am"
}, {
  "description": "ccc",
  "time": "4:00pm"
}, {
  "description": "ddd",
  "time": "6:15pm"
}, {
  "description": "eee",
  "time": "1:10am"
}, {
  "description": "fff",
  "time": "5:00pm"
}];

example.sort(function(a, b) {
  // get time time from string 
  // then get am or pm from string and append
  // both can be done using slice method
  return Date.parse('1970/01/01 ' + a.time.slice(0, -2) + ' ' + a.time.slice(-2)) - Date.parse('1970/01/01 ' + b.time.slice(0, -2) + ' ' + b.time.slice(-2))
});

console.log(example);

【讨论】:

    【解决方案2】:

    如果您将时间转换为小时和分钟(小时应该是 24 小时格式),您甚至不需要使用 Date 构造函数。

    类似这样的:

    example.map(c => {
      var time = c.time.substring(0,c.time.length - 2);
      var am_pm = c.time.slice(-2);
    
      var hours = parseInt(time.split(':')[0]);
      var minutes = parseInt(time.split(':')[1]);
    
      if (hours === 12 && am_pm.toLowerCase() === 'am') {
        hours = 0;
      } else if (hours < 12 && am_pm.toLowerCase() === 'pm') {
        hours += 12;
      }
    
      // save hours and minutes
      c.hours = hours;
      c.minutes = minutes;
    
      return c;
    }).sort((a,b) => {
      return (a.hours * 100 + a.minutes) - (b.hours * 100 + b.minutes); 
    });
    

    注意,这会通过添加 hoursminutes 属性来修改 examples 数组。

    【讨论】:

      【解决方案3】:

      您可以尝试以 24 小时格式计算值并进行相应的排序。

      逻辑:

      1. 检查pm是否存在于字符串中
      2. 如果是且小时不是12,则加12
      3. 否则返回号码

      var example=[{description:"aaa",time:"12:15pm"},{description:"bbb",time:"10:10am"},{description:"ccc",time:"4:00pm"},{description:"ddd",time:"6:15pm"},{description:"eee",time:"1:10am"},{description:"fff",time:"5:00pm"}];
      
      example.sort(function(a,b){
        var t1 = get24HrFormat(a.time);
        var t2 = get24HrFormat(b.time);
        return t1>t2 ? 1 : t1<t2 ? -1 : 0;
      });
      
      function get24HrFormat(str){
        var _t = str.split(/[^0-9]/g);
        _t[0] =+_t[0] + (str.indexOf("pm")>-1 && +_t[0]!==12 ? 12: 0);
        return _t.join("");
      }
        
      document.write("<pre>" + JSON.stringify(example,0,4)+ "</pre>")

      【讨论】:

        【解决方案4】:

        您需要在am/pm 之前添加一个space 才能确定有效日期。

        我们可以在比较之前把它放在排序方法中,如下所示。

        example.sort(function(a,b){
          return new Date('1970/01/01 ' + a.time.replace(/(am|pm)/,' $1'))
         - new Date('1970/01/01 ' + b.time.replace(/(am|pm)/,' $1'))
        })
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-06-27
          相关资源
          最近更新 更多