Tengo una tabla html como esta y una función de clasificación para ordenar la columna "r" numéricamente.
función:
function sortTablee(dir) { var table, rows, switching, i, x, y, shouldSwitch, switchcount = 0; table = document.getElementById("myTable"); switching = true; while (switching) { switching = false; rows = table.rows; for (i = 1; i < (rows.length - 1); i++) { shouldSwitch = false; x = rows[i].getElementsByTagName("TD")[1]; y = rows[i + 1].getElementsByTagName("TD")[1]; if (dir == "asc") { if (x.innerHTML > y.innerHTML) { shouldSwitch = true; break; } } else if (dir == "des") { if (x.innerHTML < y.innerHTML) { shouldSwitch = true; break; } } } if (shouldSwitch) { rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); switching = true; switchcount++; } } }Todo está bien al ordenar de manera descendente, pero al ordenar de manera ascendente, en lugar de mostrar
2.5 4 5prioriza los guiones, mostrando los números en la parte inferior. ¿Hay alguna manera de priorizar siempre y mostrar los números en la parte superior y luego llegar a los guiones?
Tal vez verifique el '-' y cámbielo siempre. Entonces todos - son empujados hacia atrás
function sortTablee(dir, w, r, ff, gg) { var table, rows, switching, i, x, y, shouldSwitch, switchcount = 0; table = document.getElementById("myTable"); switching = true; while (switching) { switching = false; rows = table.rows; for (i = 1; i < (rows.length - 1); i++) { shouldSwitch = false; x = rows[i].getElementsByTagName("TD")[1]; y = rows[i + 1].getElementsByTagName("TD")[1]; if (dir == "asc") { if(x.innerHTML == '-'){ shouldSwitch = true; break; } else if (x.innerHTML > y.innerHTML) { shouldSwitch = true; break; } } else if (dir == "des") { if(x.innerHTML == '-'){ shouldSwitch = true; break; } else if (x.innerHTML < y.innerHTML) { shouldSwitch = true; break; } } } if (shouldSwitch) { rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); switching = true; switchcount++; } } }