Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
363 views
in Technique[技术] by (71.8m points)

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

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 .

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

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


}); 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...