javascript 以“09:00 AM”格式对包含时间的字符串数组进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17064603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:58:04  来源:igfitidea点击:

Sort string array containing time in format '09:00 AM'?

javascriptjquery

提问by user2458935

I am trying to sort my array.

我正在尝试对我的数组进行排序。

The array consists of data in time format.

该数组由时间格式的数据组成。

Array:

大批:

'9:15 AM', '10:20 AM', '02:15 PM'

How should I sort it ?

我应该如何排序?

I'm getting this data usig json service & using it to list events in jquery mobile's listview . but I want to sort events by time .

我正在使用 json 服务获取此数据并使用它在 jquery mobile 的 listview 中列出事件。但我想按时间对事件进行排序。

UPDATE: HOW I SORTED DATA FROM JSON BY BOTH DATE AND TIME:

更新:我如何按日期和时间从 JSON 中排序数据:

For my particular problem of sorting data got using json by date & time I done like this :

对于我按日期和时间使用 json 对数据进行排序的特殊问题,我是这样做的:

$.getJSON(serviceURL + 'read.php?month_no='+month_no, function(data) {


        events = data.data;

        events.sort(function(a,b){
            a = new Date(a.event_date+' '+a.event_time);
            b = new Date(b.event_date+' '+b.event_time);
            return a<b?-1:a>b?1:0;
       });


}); 

回答by Shobhit Sharma

Try this

试试这个

var times = ['01:00 am', '06:00 pm', '12:00 pm', '03:00 am', '12:00 am'];

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

console.log(times);

回答by Dustin Silk

My solution (For times formated like "11:00", "16:30"..)

我的解决方案(对于格式为“11:00”、“16:30”..的时间)

sortTimes: function (array) {
    return array.sort(function (a, b) {
        if (parseInt(a.split(":")[0]) - parseInt(b.split(":")[0]) === 0) {
            return parseInt(a.split(":")[1]) - parseInt(b.split(":")[1]);
        } else {
            return parseInt(a.split(":")[0]) - parseInt(b.split(":")[0]);
        }
    })
}

In case someone wanted to know haha

万一有人想知道哈哈

回答by NINCOMPOOP

Implement the sort(compare)function and compare the date string using any arbitrary date :

实现sort(compare)函数并使用任意日期比较日期字符串:

Array.sort(function (a, b) {
    return Date.parse('01/01/2013 '+a) - Date.parse('01/01/2013 '+b)
});

01/01/2013is any arbitrary date.

01/01/2013是任意日期。

回答by rafaelcastrocouto

var a = ['9:15 AM', '10:20 AM', '02:15 PM'];

var sort = function(a){
  var sa = [],
      d = new Date(),
      ds = d.toDateString();

  for(var i = 0; i < a.length; i++){
    d = new Date(ds + ' ' + a[i]);
    sa.push(d);
  }

  sa.sort(function(a, b){return a.getTime() - b.getTime();})
  return sa;
}

回答by scottmgerstl

function sortTimes(arrayOfTimes) {

    return arrayOfTimes.sort((a, b) => {
        const aParts = getNumericParts(a);
        const bParts = getNumericParts(b);

        // Sorts by hour then minute
        return aParts[0] - bParts[0] || aParts[1] - bParts[1];
    });

    function getNumericParts(time) {
        // accounts formats of 9:15 AM and 09:15:30 but does not handle AM/PM in comparison
        return time.split(' ')[0].split(':').map(x => +x);
    }
}

Here's a more concise and performant variation of Dustin Silk's answer. It takes into account formats of 9:15 AM, 09:15, and 09:15:30 though it does not sort based on seconds. You could add that by using || aParts[2] - bParts[2]as a part of the return statement.

这是 Dustin Silk 答案的更简洁、更高效的变体。它考虑了 9:15 AM、09:15 和 09:15:30 的格式,但它不基于秒进行排序。您可以通过将其|| aParts[2] - bParts[2]用作 return 语句的一部分来添加它。

sortTimes(['08:00', '09:00', '05:00', '08:15', '08:00']) 
// Output ["05:00", "08:00", "08:00", "08:15", "09:00"]