Hello i’m using jquery to sort a table on click on the “th” tag, my code working well but only with numbers and words, but not with the date, is there anything wrong in the code :
<table>
<thead>
<tr>
<th>head1</th>
<th>head2</th>
<th>head3</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>b</td>
<td>13/03/1998</td>
</tr>
<tr>
<td>3</td>
<td>a</td>
<td>02/01/2005</td>
</tr>
<tr>
<td>2</td>
<td>c</td>
<td>10/12/2022</td>
</tr>
</tbody>
</table>
and the js :
$(document).on('click', 'th', 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();
}
table.children('tbody').empty().html(rows);
});
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).text();
}
thank you in advance