0

I have problem while sorting decimal values table sorter grid. When I try to sort distance values in the table the distance sort as follows :

distance
0.35
0.76
1.36
1.75
10.36
100.66
2.33

$(document).ready(function() {
// extend the default setting to always include the zebra widget.
$.tablesorter.defaults.widgets = ['zebra'];
// extend the default setting to always sort on the first column
$.tablesorter.defaults.sortList = [[0,0]];
$.tablesorter.defaults.debug=true;
  $("table#list").tablesorter();
  });
  $(document).ready(function() {

    $("table#list").tablesorterPager({container: $("#pager")});
    });

Can any one tell me what's the solution to prevent wrong sorting ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • I guess this happens because the values are compared as strings but should be parsed as numbers before comparing them. – Manuel Leuenberger Nov 29 '12 at 12:58
  • 1
    Read the manual. http://tablesorter.com/docs/example-meta-parsers.html – Salman A Nov 29 '12 at 12:59
  • 1
    Having looked into this it should recognise decimal columns automatically. Can you set up a fiddle (http://jsfiddle.net/) to demonstrate your problem? It may be something in your html that is confusing it... – Chris Nov 29 '12 at 13:06

1 Answers1

1

Try setting the sorter to digit in the header option:

$(function() {
  $("table#list").tablesorter({
    widgets  : ['zebra'],
    sortList : [[0,0]]
    headers  : {
      0 : { sorter : 'digit' }
    }
  }).tablesorterPager({
    container: $("#pager")
  }); 
});
Mottie
  • 84,355
  • 30
  • 126
  • 241
  • I tried `...` but it was ignored. Thanks for this version which works – mplungjan Dec 18 '12 at 12:45
  • @mplungjan That code will only work if the metadata plugin is included. Or you could check out my [fork of tablesorter](http://mottie.github.com/tablesorter/docs/) which allows you to set the class name in the header ``. – Mottie Dec 18 '12 at 14:28
  • I do not see anything about including a plugin [here](http://tablesorter.com/docs/example-meta-parsers.html) - and the link to the metadata plugin on your fork is dead --------------------------- However do you by any chance know the answer to this one: http://stackoverflow.com/questions/13933681/changing-the-way-table-is-sorted-jquery-tablesorter/13934321#13934321 – mplungjan Dec 18 '12 at 15:15
  • 1
    If you look at the page source you'll see the metadata plugin is being loaded: ``. Which link is broken? Oh and the metadata plugin is included with the download of tablesorter. – Mottie Dec 18 '12 at 16:47