Hello, I'm trying to make a soccer league standing in my schools.
<html> <body onload="sortTable(8)">
<table id="myTable" style="width: 100%;">
<caption><b>GRUP E</b></caption>
<tr>
<th>Team</th>
<th>Main</th>
<th>M</th>
<th>S</th>
<th>K</th>
<th>GS</th>
<th>GA</th>
<th>GD</th>
<th>Pts</th>
</tr>
<tr>
<td><b>AV</b></td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>5</td>
<td>0</td>
<td>5</td>
<td>3</td>
</tr>
<tr>
<td><b>Cos2</b></td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>5</td>
<td>-5</td>
<td>0</td>
</tr>
<tr>
<td><b>S4C</b></td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>6</td>
<td>-4</td>
<td>0</td>
</tr>
<tr>
<td><b>SociusDeux</b></td>
<td>1</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>6</td>
<td>2</td>
<td>4</td>
<td>3</td>
</tr>
<tr>
<td><b>XI IPA 5</b></td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</table>
<script>
function sortTable(n) {
var table, rows, switching, switching2, i, x, y, a, b, shouldSwitch, dir, switchcount = 0;
table = document.getElementById("myTable");
switching = true;
dir = "asc";
while (switching) {
switching = false;
rows = table.getElementsByTagName("TR");
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
if (dir == "asc") {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) {
shouldSwitch= true;
break;
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
switchcount ++;
}
}
switching2 = true;
dir = "asc";
while (switching2) {
switching2 = false;
rows = table.getElementsByTagName("TR");
for (i = 1; i < (rows.length - 1); i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n];
a = rows[i].getElementsByTagName("TD")[n - 1];
b = rows[i + 1].getElementsByTagName("TD")[n - 1];
if (dir == "asc") {
if (x.innerHTML.toLowerCase() == y.innerHTML.toLowerCase()) {
if(a.innerHTML.toLowerCase() < b.innerHTML.toLowerCase()) {
shouldSwitch= true;
break;
}
}
}
}
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
switchcount ++;
}
}
}
</script> </body> </html>
This is my script, i thought that nothing is wrong, but it is still not sorted correctly, can anyone please help me? ^^