I have sorted my html table, and I wanted to do a subsort. cant seems to figure out what's wrong with my code, been stuck here for a while now. I am trying to sort table on columns 4 then if there is any value identical sort them by id
let table = document.querySelector('tbody')
function sort() {
let tr = table.children;
let test1 = [...tr]
.sort((a, b) => parseInt(b.children[3].innerHTML) - parseInt(a.children[3].innerHTML))
.map(el => table.append(el));
let test2 = [...tr]
.sort((a, b) => {
if (a.children[3].innerHTML === b.children[3].innerHTML)
return parseInt(a.children[0].innerHTML) - parseInt(b.children[0].innerHTML)
}
})
.map(el => table.append(el));
};
==HTML==
<table class="table">
<thead>
<tr class='table_row'>
<th>id</th>
<th>header2</th>
<th>header3</th>
<th>header4</th>
</tr>
</thead>
<tbody>
<tr>
<td>5</td>
<td>data1</td>
<td>jone</td>
<td>170</td>
</tr>
<tr>
<td>1</td>
<td>data2</td>
<td>josh</td>
<td>170</td>
</tr>
<tr>
<td>2</td>
<td>data3</td>
<td>adrian</td>
<td>270</td>
</tr>
<tr>
<td>8</td>
<td>data4</td>
<td>merry</td>
<td>70</td>
</tr>
</tbody>
</table>
return a.children[3].innerHTML - b.children[3].innerHTML || parseInt(a.children[0].innerHTML) - parseInt(b.children[0].innerHTML)