Sorting troubles

Sorting troubles

dttdtt Posts: 2Questions: 0Answers: 0
edited February 2010 in Plug-ins
Hi,

I seem to have exactly the same problem as posted in "[Sink] Bamboozled over Date sorting" where the author says he needs to put his code directly into the main DataTables js file to get it to work. I have written my own sorter to sort numbers formatted with the thousands separator eg "23,456" (adapted from the example on the website using comma as decimal)
[code]
jQuery.fn.dataTableExt.aTypes.push(
function ( sData )
{

var sValidChars = "0123456789-,.";
var Char;
var bDecimal = false;

/* Check the numeric part */
for ( i=0 ; i

Replies

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    Phew - I should have known the answer to that one immediately, but I had forgotten about how this should be correctly set up. Found one bug in DataTables and one two bugs in my plug-in sorting demo (now fixed at http://datatables.net/examples/plug-ins/sorting_plugin.html ). Fortunately the DataTables bug won't impact you here I don't think!

    The trick here is that there is an interaction between the numeric and xyz detection. If DataTables find a column which does not fully match a type description, then it falls back to 'string'. And in this case, 'numeric' was being checked first, so any unformatted integers would return 'numeric', but you of course want xyz. As soon as it hits one which doesn't match numeric, but is xyz, it falls back to string!

    So you need to make 'xyz' a higher priority. And this is done simply by adding your type detection function to the start of the array, rather than at the end. Array.unshift() does this (although note it won't work in IE6 - a simple array concat will be needed if you want IE6 support). My example has been updated for this.

    Regards,
    Allan
  • dttdtt Posts: 2Questions: 0Answers: 0
    Great! Thank you.
This discussion has been closed.