I have a table of dates
<table>
<tr>
<th>Date</th>
</tr>
<tr>
<td>01/03/2018 02:31 AM</td>
</tr>
<tr>
<td>01/13/2018 03:00 AM</td>
</tr>
<tr>
<td>09/02/2017 02:31 AM</td>
</tr>
<tr>
<td>11/29/2017 09:30 PM</td>
</tr>
<tr>
<td>03/01/2018 03:00 AM</td>
</tr>
</table>
that I want to order chronologically. I found another thread that recommended using:
$('th').click(function(){
var table = $(this).parents('table').eq(0)
var rows = table.find('tr:gt(0)').toArray().sort(comparer($(this).index()))
this.asc = !this.asc
if (!this.asc){rows = rows.reverse()}
for (var i = 0; i < rows.length; i++){table.append(rows[i])}
})
function comparer(index) {
return function(a, b) {
var valA = getCellValue(a, index), valB = getCellValue(b, index)
return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : valA.localeCompare(valB)
}
}
function getCellValue(row, index){ return $(row).children('td').eq(index).html() }
but output doesn't order newest to oldest correctly.
I get this:
Date
01/03/2018 02:31 AM
01/13/2018 03:00 AM
03/01/2018 03:00 AM
09/02/2017 02:31 AM
11/29/2017 09:30 PM
when it should start with the newest date first. And advice on how I can get these sorted correctly?