Search code examples
javascriptjqueryregextablesorter

Tablesorter specific sort with french number


I'm using tablesorter for sort different type of number and string, but in this number i have number like : (with commas and space)

200,08€
1 201,56€
1 521 120,00€

I must be sort like

1 521 120,00€
1 201,56€
200,08€

and inverse.

I try a parser :

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

but it doesn't work, have you an idea of why ? thanks a lot !


Solution

  • There are two issues with the parser: (1) The comma should be replaced with a decimal point, and (2) the spaces need to be removed.

    Try this update (demo)

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