按格式'09:00 AM'排序包含时间的字符串数组? [英] Sort string array containing time in format '09:00 AM'?

查看:145
本文介绍了按格式'09:00 AM'排序包含时间的字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to sort my array.

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

The array consists of data in time format.

数组:

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

我应该如何排序?

我收到了这个数据使用json服务&用它来列出jquery mobile的listview中的事件。但是我希望按时间对事件进行排序。

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分类数据:

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

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

$.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;
       });


}); 


推荐答案

试试这个

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);

这篇关于按格式'09:00 AM'排序包含时间的字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆