How to use "parse Float" in JavaScript

I have a table and I got it to sort my table headers, but I have a few columns with 100 different numbers, and it is not sorting correct. I did look at parse float, but cannot figure it out.
How do you use it if numbers are like this. 0.0000000000, 12.00, 12,999,999,999,999,999.01234567. this is just example.
Di I have to change table info also.
Here is my script that I have

/**
 * Sorts a HTML table.
 *
 * @param {HTMLTableElement} table The table to sort
 * @param {number} column The index of the column to sort
 * @param {boolean} asc Determines if the sorting will be in ascending
 */
 function sortTableByColumn(table, column, asc = true) {
    const dirModifier = asc ? 1 : -1;
    const tBody = table.tBodies[0];
    const rows = Array.from(tBody.querySelectorAll("tr"));

    // Sort each row
    const sortedRows = rows.sort((a, b) => {
        const aColText = a.querySelector(`td:nth-child(${ column + 1 })`).textContent.trim();
        const bColText = b.querySelector(`td:nth-child(${ column + 1 })`).textContent.trim();

        return aColText > bColText ? (1 * dirModifier) : (-1 * dirModifier);
    });

    // Remove all existing TRs from the table
    while (tBody.firstChild) {
        tBody.removeChild(tBody.firstChild);
    }

    // Re-add the newly sorted rows
    tBody.append(...sortedRows);

    // Remember how the column is currently sorted
    table.querySelectorAll("th").forEach(th => th.classList.remove("th-sort-asc", "th-sort-desc"));
    table.querySelector(`th:nth-child(${ column + 1})`).classList.toggle("th-sort-asc", asc);
    table.querySelector(`th:nth-child(${ column + 1})`).classList.toggle("th-sort-desc", !asc);
}

document.querySelectorAll(".table-sortable th").forEach(headerCell => {
    headerCell.addEventListener("click", () => {
        const tableElement = headerCell.parentElement.parentElement.parentElement;
        const headerIndex = Array.prototype.indexOf.call(headerCell.parentElement.children, headerCell);
        const currentIsAscending = headerCell.classList.contains("th-sort-asc");

        sortTableByColumn(tableElement, headerIndex, !currentIsAscending);
    });
});
type or paste code here

Here is my table header

 <table class="table table-sortable">
             <thead>
                    <tr>
                        <th height="50">Name</th>
                        <th height="50">WWW</th>
                        <th height="50">CHAIN</th>
                        <th height="50">Value</th>
                        <th height="50">Day</th>
                        <th height="50">Week</th>
                        <th height="50">Month</th>
                        <th height="50">Year</th>
                        <th height="50">Coins</th>
                        <th height="50">Button</th>
                        <th height="50">USD</th>
                        <th height="50">Button</th>
                        <th height="50">ZAR</th>
                    </tr>
            </thead>
            <tbody>
                    <tr>

Are these values strings or numbers? If you want them as numbers, Iā€™d guess this is where you need parseFloat

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.