0

I'm trying to implement a javascript sort function for my HTML table, but I couldn't make it works with dates.

    <script>
    function sortTable(table, col, reverse) {
        var tb = table.tBodies[0],
            tr = Array.prototype.slice.call(tb.rows, 0),
            i;
        reverse = -((+reverse) || -1);
        tr = tr.sort(function (a, b) { 
            return reverse 
                * (a.cells[col].textContent.trim().localeCompare(b.cells[col].textContent.trim(), undefined, {numeric: true}));
        });
        for(i = 0; i < tr.length; ++i) tb.appendChild(tr[i]); 
    }

    function makeSortable(table) {
        var th = table.tHead, i;
        th && (th = th.rows[0]) && (th = th.cells);
        if (th) i = th.length;
        else return;
        while (--i >= 0) (function (i) {
            var dir = 1;
            th[i].addEventListener('click', function () {sortTable(table, i, (dir = 1 - dir))});
        }(i));
    }

    function makeAllSortable() {
        var t = document.getElementById('tablediv01').getElementsByTagName('table'), i = t.length;
        while (--i >= 0) makeSortable(t[i]);
    };

    makeAllSortable();
    </script>

I need help here, what code should I put to make it sort also the dates

   (a.cells[col].textContent.trim().localeCompare(b.cells[col].textContent.trim(), undefined, {numeric: true}));
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

0 Answers0