備忘録

備忘録

jquery.tablesorter カンマ区切りのソートに対応させる

百聞は一見にしかず
https://jsfiddle.net/86rgf7eu/

$(function() {
    $.tablesorter.addParser({
        id: "fancyNumber",
        is: function(s) {
            return /^[0-9]?[0-9,\.]*$/.test(s);
        },
        format: function(s) {
            return $.tablesorter.formatFloat(s.replace(/,/g, ''));
        },
        type: "numeric"
    });

    $("#users").tablesorter({
        widgets: ['zebra'],
        headers: {
            0: {
                sorter: 'fancyNumber'
            }
        }
    });
});
<table id="users" class="tablesorter">
    <thead>
        <th>id</th>
        <th>name</th>
    </thead>
    <tbody>
        <tr>
            <td>1,040</td>
            <td>name1</td>
        </tr>
        <tr>
            <td>1,050</td>
            <td>name2</td>
        </tr>
        <tr>
            <td>1,060</td>
            <td>name3</td>
        </tr>
        <tr>
            <td>1,100</td>
            <td>name4</td>
        </tr>
        <tr>
            <td>1,200</td>
            <td>name5</td>
        </tr>
    </tbody>
</table>