Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 604
Fons Avatar asked Sep 22 '09 12:09

Fons


3 Answers

Hopefully this will help someone if they find this post, in tablesorter you can now simply use.

$(".table").tablesorter({
     headers: {
         5: { sorter: 'digit' } // column number, type
     }
 });
like image 85
Dave Avatar answered Nov 13 '22 12:11

Dave


<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

like image 20
Fons Avatar answered Nov 13 '22 11:11

Fons


This may have been obvious to others (not to me) but to get the solution working with the {sorter: 'digit'} metadata you need to use the jQuery metadata plugin.

like image 5
Jeff Steil Avatar answered Nov 13 '22 13:11

Jeff Steil