Can't create a custom sorting function

Can't create a custom sorting function

DubslowDubslow Posts: 7Questions: 0Answers: 0
edited July 2012 in General
I'm trying to use this example: http://datatables.net/release-datatables/examples/plug-ins/sorting_sType.html
But for whatever reason, my way isn't working; I'm fairly sure it's mostly due to my own lack of knowledge about DataTables/jQuey/JS in general, so this should be an easy fix. Here's the code:
[code]function specialSort(a, b) {
x = a.indexOf('-') > -1; // true for date
y = a.indexOf('-') > -1; // true for date
if( x ) {
if( y ) {
return (a < b) ? -1 : ( (a > b) ? : 1 : 0);
} else {
return -1;
}
} else if( y ) {
return 1;
} else {
a = parseInt(a);
b = parseInt(b);
return (a < b) ? -1 : ( (a > b) ? : 1 : 0);
}
}
jQuery.fn.dataTableExt.oSort['special-asc'] = specialSort; // Is this the right syntax for assigning functions? I tried some other variants, but none of those worked...
jQuery.fn.dataTableExt.oSort['special-desc'] = function(a, b) { return -specialSort(a, b); };
/*snip...*/
"aoColumnDefs": [
{ "sType": "special", "aTargets": [8] }
]
[/code]
Whenever I add these lines to my code though, my table goes blank except for the headers. (And my invisible column headers appear.) I'm pretty sure it's just a syntax thing, but I don't know what it might be.

Thanks for the great work Allan!

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    I'm guessing that the JS will be throwing an error. What is that error (look at the console in Firebug or Inspector)?

    Allan
  • DubslowDubslow Posts: 7Questions: 0Answers: 0
    See, it's a testament to how ignorant of JS I am that it took me a good 15 minutes to figure what and where the console was. Turns out that there's two stupid syntax errors in the ?: statements (namely I actually have a '?' and a ':' right next to each other, without an expression between them). I then managed to work out the rest of the problems myself, thanks to the newfound console. :) Sorry to bother you with such trivialities, but at least it won't happen again. :P
This discussion has been closed.