Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
Sort table by two columns vanilla js

How can i sort my table by two columns ( Name, Available) on page load? I can use vanilla js

        <table class="table_sort">
    <thead>
    <tr>
        <th class="sorted-asc">Name</th>
        <th>Genre</th>
        <th>Publish year</th>
        <th>Quanity</th>
        <th class="available">Available</th>
    </tr>
    </thead>

    <tbody id="tbody">
    <tr>
        <td>aname1</td>

        <td>genre1</td>

        <td>year1</td>
        <td>quantity1</td>
        <td>2</td>
    </tr>
    <tr>
        <td>name1</td>

        <td>genre1</td>

        <td>year1</td>
        <td>quantity1</td>
        <td>1</td>
    </tr>
    <tr>
        <td>aname1</td>

        <td>genre1</td>

        <td>year1</td>
        <td>quantity1</td>
        <td>10</td>
    </tr>
    <tr>
        <td>aname1</td>

        <td>genre1</td>

        <td>year1</td>
        <td>quantity1</td>
        <td>6</td>
    </tr>

    </tbody>
</table>

I try to sort my table by two columns: Name, Available but it does not work.

    document.addEventListener('DOMContentLoaded', () => {
const table = document.querySelector('.table_sort');
const indexToSorting = [...table.tHead.rows[0].cells].findIndex(cell => cell.classList.contains('sorted-asc'));
const availableIndexes = [...table.tHead.rows[0].cells].findIndex(cell => cell.classList.contains('available'));
const sortedRows = [...table.tBodies[0].rows].sort((rowA, rowB) => {
    let cellC;
    let cellD;
    const sortedRowsByAvailable = [...table.tBodies[0].rows].sort((rowC, rowD) => {
        cellC = rowC.cells[availableIndexes].innerText;
        cellD = rowD.cells[availableIndexes].innerText;
        const availableComparison = cellC.localeCompare(cellD);
        return availableComparison;

    });
    const cellA = rowA.cells[indexToSorting].innerText;
    const cellB = rowB.cells[indexToSorting].innerText;
    const nameComparison = cellA.localeCompare(cellB);
    return nameComparison !== 0 ? nameComparison : sortedRowsByAvailable
});

table.tBodies[0].append(...sortedRows);

});

My table is sorted by name, but i need to sort it by columns: name, available. Where is my mistake? I don't understand, please, help me

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

TBH, I didn't understand why you did cellA - cellB, but you need to add a comparison for Available cells in case cellA and cellB are equal.


const rowAAvailable = rowA.cells[indexToSorting].innerText;
const rowBAbailable = rowB.cells[indexToSorting].innerText;
const nameComparison = cellA.localeCompare(cellB);
const availableComparison = rowAAvailable.localeCompare(rowBAbailable); // sorry for terrible naming, but you get the idea
return nameComparison !== 0 ? nameComparison : availableComparison

This will sort your table by name as the first priority and available the second one.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda