Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  14] [ 2]  / answers: 1 / hits: 23554  / 11 Years ago, wed, june 12, 2013, 12:00:00

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


});

More From » jquery

 Answers
8

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

[#77666] Tuesday, June 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mustafaericho

Total Points: 322
Total Questions: 103
Total Answers: 110

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
mustafaericho questions
Mon, May 31, 21, 00:00, 3 Years ago
Sun, May 23, 21, 00:00, 3 Years ago
Sat, Feb 13, 21, 00:00, 3 Years ago
Sat, Jan 2, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
;