Table sorting numbers with commas

Table sorting numbers with commas

brihibrihi Posts: 3Questions: 0Answers: 0
edited April 2010 in General
Hi,
Table sorting doesn’t work
Below is an example of how the numbers sort in one of the columns. How do I get the table to sort the data properly? I am using WordPress's wp-table reloaded plugin which uses DataTable coding.

1
1,861
119,825
126,631
140
163
2049
2254
etc…

Is there DataTables coding that exists that I can extend to the WordPress wp-table reloaded plugin?

Thanks,
-BrianP

Replies

  • spydinkspydink Posts: 4Questions: 0Answers: 0
    I had this issue too. I made a copy of the currency plugin found here:

    http://datatables.net/plug-ins/sorting

    and removed the /* Remove the currency sign */ sections. Using this slightly modified plugin now sorts this type of data just fine.

    I'm not using the wp-table plugin so I can't comment on that. I am using ThemeRoller and TableTools though.
  • brihibrihi Posts: 3Questions: 0Answers: 0
    Hi,

    Where do I copy this code [as per your instructions above]?

    jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
    02. /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
    03. var x = a == "-" ? 0 : a.replace( /,/g, "" );
    04. var y = b == "-" ? 0 : b.replace( /,/g, "" );
    05.

    10. /* Parse and return */
    11. x = parseFloat( x );
    12. y = parseFloat( y );
    13. return x - y;
    14.};
    15.
    16.jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
    17. /* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
    18. var x = a == "-" ? 0 : a.replace( /,/g, "" );
    19. var y = b == "-" ? 0 : b.replace( /,/g, "" );
    20.
    24.
    25. /* Parse and return */
    26. x = parseFloat( x );
    27. y = parseFloat( y );
    28. return y - x;
    29.};
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Hi all,

    The way to make use of sorting plug-ins such as the currency plug-in is described on the sorting plug-in page: http://datatables.net/plug-ins/sorting . Basically you just include the sorting plug-in code after your script tag for jQuery and DataTables, but before you actually make the $().dataTable() call.

    Here is an example of how to do it with the numeric comma plug-in: http://datatables.net/examples/plug-ins/sorting_sType.html

    Regards,
    Allan
  • amp343amp343 Posts: 1Questions: 0Answers: 0
    Finally understand why the numeric comma plugin isn't sorting my numbers like 100,000 and 6,483 correctly -- that plugin interprets commas as decimals, as in "1,7" for "1.7", NOT as characters delimiting every third digit in a large number, as in 340,000,000. Hope that observation might help others banging their heads, as I was, expecting the Numeric Comma plugin to sort comma-delimited numbers, not commas-as-decimals.

    Looks like the solution is to modify an existing plugin for now, as the first commenter suggested.
This discussion has been closed.