Custom sort not working

Custom sort not working

jeffwjeffw Posts: 38Questions: 5Answers: 0
edited July 2013 in General
Having some trouble with hidden title numeric sorting. Here's a simple markup example:


Sort

one test 1
two test 2
three test 3



Should sort as 1, 2, 3. But it's sorting alpha ('one test', 'three test', 'two test'). The 'th' header has a 'customsort' class, and aTargets links it to the sType title-numeric. The table renders fine, the custom sort just isn't kicking in properly. Must be something basic I'm doing wrong but I can't find it.

Many thanks to Allan for such a great tool. And thanks to anyone else who can point me in the right direction.

Here's my javascript (inside a document.ready in the actual document):

$.extend( $.fn.dataTableExt.oSort, {
"title-numeric-pre": function ( a ) {
var x = a.match(/title="*(-?[0-9\.]+)/)[1];
return parseFloat( x );
},

"title-numeric-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},

"title-numeric-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );


$('table.sortable').dataTable({
"bPaginate": false, "bLengthChange": false,
"bFilter": false, "bSort": true, "bInfo": false, "bAutoWidth": false,

"aoColumnDefs": { "sType": "title-numeric", "aTargets": ['customsort'] }

});

Replies

  • jeffwjeffw Posts: 38Questions: 5Answers: 0
    Posted test case at

    http://live.datatables.net/ubiqaq/4/edit
  • jeffwjeffw Posts: 38Questions: 5Answers: 0
    Changed example slightly to use sSortDataType instead of sType; same results. Example at

    http://live.datatables.net/ubiqaq/8/edit

    $('table.sortable').dataTable({
    "bPaginate": false,
    "bLengthChange": false,
    "bFilter": false, "bSort": true, "bInfo": false, "bAutoWidth": false,
    "aoColumnDefs": { "sSortDataType": "title-numeric", "aTargets": ['customsort'] }
    });

    Adding "sType": "numeric" didn't help either:
    http://live.datatables.net/ubiqaq/9/edit
This discussion has been closed.