I have 3 columns:
Column1 Column2 Column3
111111 111111 1
222222 222222 2
333333 333333 3
444444 444444 4
555555 555555 5
666666 666666 6
If I click on "Column1" or "Column2", the table is correctly ordered from least to greatest. But if I click on "Column3" it doesn't.
The code works fine, but when I have more numeric data ie. "1" and "11" sorts the number "11" as one. Example. It orders me 1, 11, 14, 15, 2, 3, 4...
This is my Javascript:
$(document).ready(() => {
$('th').each(function (columna) {
$(this).click(function () {
let registros = $('table').find('tbody > tr').get();
registros.sort(function (a, b) {
let valor1 = $(a).children('td').eq(columna).text().toUpperCase();
let valor2 = $(b).children('td').eq(columna).text().toUpperCase();
return valor1 < valor2 ? -1 : valor1 > valor2 ? 1 : 0
});
$.each(registros, function (indice, elemento) {
$('tbody').append(elemento);
});
});
});
});
All data is recovered from database as string
, and I tried to convert "Column3" in int
but nothing happens
Any solution for that?