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

145
Visualizações
Sort HTML table by Javascript

I have the following HTML table which I wanted to be sorted by data-sku=""

( function() {
        $("table tbody tr").sort(sort_table).appendTo('table tbody');
            function sort_table(a, b) {
            return ($(b).data('sku')) < ($(a).data('sku')) ? 1 : -1;
        }
} )();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="abp-product-42" data-id="42" data-sku="A-04-0002"><td>A-04-0002</tr>
<tr class="abp-product-21" data-id="21" data-sku="A-04-0011"><td>A-04-0011</td></tr>
<tr class="abp-product-391" data-id="391" data-sku="A-02-0008"><td>A-02-0008</td></tr>
<tr class="abp-product-393" data-id="393" data-sku="A-02-0007"><td>A-02-0007</td></tr>
<tr class="abp-product-40" data-id="40" data-sku="A-04-0010"><td>A-03-0010</td></tr>
<tr class="abp-product-390" data-id="390" data-sku="A-03-0003"><td>A-04-0003</td></tr>
</tbody>
</table>

But somehow it doesn't work - what I'm doing wrong or what I'm missing?

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

0

Your code is working fine.
Just, The last 2 rows attribute data-sku are not match with it's cell content.

<tr class="abp-product-40" data-id="40" data-sku="A-04-0010">
    <td>A-03-0010</td>
</tr>
<tr class="abp-product-390" data-id="390" data-sku="A-03-0003">
    <td>A-04-0003</td>
</tr>

(function () {
    function sort_table(a, b) {
        return ($(b).attr('data-sku')) < ($(a).attr('data-sku')) ? 1 : -1;
    }
    $("table tbody tr").sort(sort_table).appendTo('table tbody');
})();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
    <tbody>
        <tr class="abp-product-42" data-id="42" data-sku="A-04-0002">
            <td>A-04-0002
        </tr>
        <tr class="abp-product-21" data-id="21" data-sku="A-04-0011">
            <td>A-04-0011</td>
        </tr>
        <tr class="abp-product-391" data-id="391" data-sku="A-02-0008">
            <td>A-02-0008</td>
        </tr>
        <tr class="abp-product-393" data-id="393" data-sku="A-02-0007">
            <td>A-02-0007</td>
        </tr>
        <tr class="abp-product-40" data-id="40" data-sku="A-03-0010">
            <td>A-03-0010</td>
        </tr>
        <tr class="abp-product-390" data-id="390" data-sku="A-04-0003">
            <td>A-04-0003</td>
        </tr>
    </tbody>
</table>

about 4 years ago · Juan Pablo Isaza Relatório

0

This will call your function in every 5 seconds like I mentioned in the comment.

window.setInterval(function(){
 sortData();
}, 5000);

function sortData(){
 $("table tbody tr").sort(sort_table).appendTo('table tbody');
            function sort_table(a, b) {
            return ($(b).data('sku')) < ($(a).data('sku')) ? 1 : -1;
        }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr class="abp-product-42" data-id="42" data-sku="A-04-0002"><td>A-04-0002</tr>
<tr class="abp-product-21" data-id="21" data-sku="A-04-0011"><td>A-04-0011</td></tr>
<tr class="abp-product-391" data-id="391" data-sku="A-02-0008"><td>A-02-0008</td></tr>
<tr class="abp-product-393" data-id="393" data-sku="A-02-0007"><td>A-02-0007</td></tr>
<tr class="abp-product-40" data-id="40" data-sku="A-04-0010"><td>A-04-0010</td></tr>
<tr class="abp-product-390" data-id="390" data-sku="A-03-0003"><td>A-03-0003</td></tr>
<tr class="abp-product-390" data-id="390" data-sku="A-05-0003"><td>A-05-0003</td></tr>
</tbody>
</table>

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