I have a table that I sort ASC by building name. The next column is the corresponding building number for the building. I have a function that will sort the table in ASC order based on the building name. I want to be able to sort by the building name and the building number at the same time. This is a snippet of the table when the page loads. The first <td>
which is the building name goes in order. However, the next <td>
which is the building number does not go in order.
<tbody class="meterList">
<tr>
<td>ACB</td>
<td>A2095</td>
</tr>
<tr>
<td>ACB</td>
<td>E1559</td>
</tr>
<tr>
<td>ACB</td>
<td>E1540</td>
</tr>
<tr>
<td>ADH</td>
<td>A0001</td>
</tr>
<tr>
<td>ADH</td>
<td>E1076</td>
</tr>
<tr>
<td>ADH</td>
<td>S0001</td>
</tr>
<tr>
<td>ADH</td>
<td>E1075</td>
</tr>
</tbody>
This is the function I use the sort the table in ASC order by building name.
$('.meterList').find('tr').sort(function(a, b) {
return $('td:first', a).text().localeCompare($('td:first', b).text());
}).appendTo($('.meterList'))
I tried doing something like this to sort by both the first and second <td>
but this didnt work.
$('.meterList').find('tr').sort(function(a, b) {
return $('td:first, td:second', a).text().localeCompare($('td:first, td:second', b).text());
}).appendTo($('.meterList'))
If anyone has any idea how I can sort by two columns any advice would be greatly appreciated!