Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
How to sort table by two columns instead of one

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!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This should do it:

const t=(tr,i)=>tr.cells[i].textContent;
$(".meterList tr").get()
  .sort((a,b)=>t(a,0).localeCompare(t(b,0)) || t(a,1).localeCompare(t(b,1)))
  .map(tr=>$(".meterList").append(tr));
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<table><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></table>

First I created a little helper function t(tr,I) that gets me the .textContent of the i-th cell for a given <tr>.

Then I collect all <tr>s in an array (.get() extracts the actuall array of DOM elements from the jQuery object) and .sort() it. The sort-callback function will first compare the first cells (index=0) of two given <tr>s and - only when the comparison returns 0 - the second comparison will take place (looking at the second cells in each <tr>).

This can be done even without creating any visible variables or constants outside the actual jQuery construct. In fact, there is not much jQuery left in the above code and it can just as easily be done without it. ;-)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda