Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incorrect sorting in TableSorter

Tags:

tablesorter

Strange behavoir in tablesorter:

$(document).ready(function() { 
    var timeInSec = function(node) { return $(node).attr("time") };
    var sortDigit = { sorter : "digit" };

    $("#agentRapport").tablesorter({ 
        headers: {
            1: sortDigit,
            2: sortDigit
        },
        textExtraction: {
             1: timeInSec,
             2: timeInSec
        },
        sortList: [[1,1]]
    }); 
}); 

Resulting sorted table looks like this (sorted the following myself like you would see it in the browser):

...
<tr>
    <td>Banana</td>
    <td time="1411252">23 min 31 sec</td>
    <td time="352813">5 min 52 sec</td>
    <td>4</td>
    <td>225</td>
</tr>
<tr>
    <td>Apple</td>
    <td time="1391952">23 min 11 sec</td>
    <td time="347988">5 min 47 sec</td>
    <td>4</td>
    <td>86</td>
</tr>
<tr>
    <td>Plum</td>
    <td time="1427192">23 min 47 sec</td>
    <td time="356798">5 min 56 sec</td>
    <td>4</td>
    <td>119</td>
</tr>
<tr>
    <td>Pear</td>
    <td time="1381072">23 min 1 sec</td>
    <td time="345268">5 min 45 sec</td>
    <td>4</td>
    <td>108</td>
</tr>
...

Is this a bug?

like image 708
myborobudur Avatar asked Apr 27 '26 02:04

myborobudur


1 Answers

If you are using the original tablesorter (v2.0.5b) from tablesorter.com, the above code will not work since the textExtraction function will not allow the targetting of specific columns.

However, you can use my fork of tablesorter which does allow this option (demo)

The fork should work without any extra functions since it also has an alpha-numeric sort (demo):

$(function () {
    $("#agentRapport").tablesorter({
        sortList: [
            [1, 1]
        ]
    });
});

But if you want to continue using the original version of tablesorter, you can use a parser, and save yourself from adding a time="#" attribute to every cell (demo):

$.tablesorter.addParser({
    id: "min&sec",
    is: function (s) {
        return false;
    },
    format: function (s, table) {
        var min = (parseInt(s.match(/(\d+)(?:\s+min)/), 10) || 0) * 60,
            sec = (parseInt(s.match(/(\d+)(?:\s+sec)/), 10) || 0);
        return min + sec;
    },
    type: "numeric"
});

$(function () {

    $("#agentRapport").tablesorter({
        headers: {
            1: { sorter: "min&sec" },
            2: { sorter: "min&sec" }
        },
        sortList: [
            [1, 1]
        ]
    });
});
like image 129
Mottie Avatar answered May 03 '26 17:05

Mottie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!