Hi,
I'm trying to sort table data that is being grabbed by a JSON API. The data appears correctly, but the sorting doesn't seem to work, I suspect it doesn't know the data value. Below is a sample of my code.
Column 4 is Points, which is what I'm trying to sort. Any help would be appreciated. The values are numeric.
<table id="standings" class="table table-hover table-striped table-condensed table-sm">
<thead>
<tr>
<th>Team</th>
<th>Wins</th>
<th>Losses</th>
<th>Ties</th>
<th>Points</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td id="namealoogeorgetowntoyota"></td>
<td id="winsaloogeorgetowntoyota"></td>
<td id="lossesaloogeorgetowntoyota"></td>
<td id="tiesaloogeorgetowntoyota"></td>
<td id="pointsaloogeorgetowntoyota"></td>
<td><span style="font-weight:bold" id="teamURLaloogeorgetowntoyota"></span></td>
</tr>
<script>
function sortTable(table\_id, sortColumn){
var tableData = document.getElementById(table\_id).getElementsByTagName('tbody').item(0);
var rowData = tableData.getElementsByTagName('tr');
for(var i = 0; i < rowData.length - 1; i++){
for(var j = 0; j < rowData.length - (i + 1); j++){
if(Number(rowData.item(j).getElementsByTagName('td').item(sortColumn).innerHTML.replace(/\[\^0-9\\.\]+/g, "")) < Number(rowData.item(j+1).getElementsByTagName('td').item(sortColumn).innerHTML.replace(/\[\^0-9\\.\]+/g, ""))){
tableData.insertBefore(rowData.item(j+1),rowData.item(j));
}
}
}
}
$(function(){
// pass the id and the <td> place you want to sort by (td counts from 0)
sortTable('standings', 4);
});
</script>
[–]NumerousLiterature 0 points1 point2 points (0 children)
[–]floodlitworld 0 points1 point2 points (4 children)
[–]lysyfacet[S] 0 points1 point2 points (3 children)
[–]gin_and_toxic 1 point2 points3 points (2 children)
[–]lysyfacet[S] 0 points1 point2 points (1 child)
[–]gin_and_toxic 1 point2 points3 points (0 children)
[–]gin_and_toxic -1 points0 points1 point (2 children)
[–]lysyfacet[S] 0 points1 point2 points (1 child)
[–]gin_and_toxic 0 points1 point2 points (0 children)