I have a list of appointments with dates and times as shown below:
[
["John Doe", "10 Sep. 2017 2:00 PM"],
["Jane Smith", "10 Sep. 2017 2:00 PM"],
["Alice Johnson", "10 Sep. 2017 1:00 PM"],
["Bob Williams", "10 Sep. 2017 8:00 AM"],
["Mark Davis", "9 Sep. 2017 2:00 PM"],
["Emily Brown", "9 Sep. 2017 2:00 PM"],
["David Lee", "9 Sep. 2017 1:00 PM"],
["Sarah Parker", "9 Sep. 2017 8:00 AM"]
]
My goal is to rearrange the appointments so that the dates are in ascending order, but the times are reversed chronologically. The desired output is:
[
["John Doe", "10 Sep. 2017 8:00 AM"],
["Alice Johnson", "10 Sep. 2017 1:00 PM"],
["John Doe", "10 Sep. 2017 2:00 PM"],
["Jane Smith", "10 Sep. 2017 2:00 PM"],
["Bob Williams", "9 Sep. 2017 8:00 AM"],
["David Lee", "9 Sep. 2017 1:00 PM"],
["Mark Davis", "9 Sep. 2017 2:00 PM"],
["Emily Brown", "9 Sep. 2017 2:00 PM"]
]
I currently have a sorting function implemented which sorts the appointments based on date. How can I modify this existing code to sort the appointments by time within each date?
function sortTable(data) {
return sortTableHelper(data);
function sortTableHelper(data) {
data = data.sort((elem1, elem2) => {
var date1 = moment(elem1[1], 'D MMM YYYY h:m A')
, date2 = moment(elem2[1], 'D MMM YYYY h:m A');
if (date1.isAfter(date2)) return 1;
return -1;
});
return data;
}
}