Search code examples
jquerytablesorter

jQuery tablesorter is not sorting number correctly


I've been trying for days now to get jQuery tablesorter correctly sort numbers in my table column.

I am using the current latest versions of both scripts.

The table is rendered fine, but sorting the numbers is not working correctly.

When I sort a number column it gives me the following results:

8 7 4 32 31 3 etc..

where you would expect: 32 31 8 etc...

I read some comments on adding extra javascript code but I can't find any good javascript examples.

The jQuery I'm using now is as follows:

$(document).ready(function()
    {
      $("#table1")
       .tablesorter(
          {
            sortList: [[0,0]],
            widthFixed: true,
            widgets: ['zebra']
          } )
    }
);

Here is my HTML:

<table id="table1" class=tablesorter>
    <thead>
        <tr>
            <th width=65>Name</th>
            <th width=40>Count</th>
        </tr>
     </thead>
     <tbody>
         <tr><td>Name_1</td><td>32</td></tr>
         <tr><td>Name_2</td><td>12</td></tr>
         <tr><td>Name_3</td><td>11</td></tr>
         <tr><td>name_4</td><td>14</td></tr>
         <tr><td>Name_5</td><td>7</td></tr>
         <tr><td>Name_6</td><td>3</td></tr>
         <tr><td>Name_7</td><td>32</td></tr>
         <tr><td>Name_8</td><td>31</td></tr>
         <tr><td>Name_9</td><td>35</td></tr>
      </tbody>
</table>

Solution

  • <th width=110 class=\"{sorter: 'digit'}\">Count</th>
    

    This solved the problem. Telling the javascript to handle the value's as a digit made the sorting work correct. Still bit silly that number values are not checked in the script as being numbers. But i guess there is a higher purpose for that in the end.

    Thanks all for your time and help

    /Fons