Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!