JSFiddle - React, Tailwind, and code Playground
JavaScript
var events = [
{
time: "12:30pm"
},
{
time: "04:30pm"
},
{
time: "8:30am"
}
];
// http://stackoverflow.com/questions/15083548/convert-12-hour-hhmm-am-pm-to-24-hour-hhmm
function convertTo24Hour(time) {
var hours = parseInt(time.substr(0, 2));
if(time.indexOf('am') != -1 && hours == 12) {
time = time.replace('12', '0');
}
if(time.indexOf('pm') != -1 && hours < 12) {
time = time.replace(hours, (hours + 12));
}
return time.replace(/(am|pm)/, '');
}
function compare(a,b) {
var atime = convertTo24Hour(a.time);
var btime = convertTo24Hour(b.time);
if (a.last_nom < b.last_nom) {
return -1;
}
if (a.last_nom > b.last_nom) {
return 1;
}
return 0;
}
events.sort(compare);