How to install Natural Sorting?

How to install Natural Sorting?

alcacervealcacerve Posts: 2Questions: 0Answers: 0
edited March 2011 in Plug-ins
Hello, I am using DataTables on my Wordpress blog to make my table sortable. I tried to use WP-Table Reloaded (Wordpress Plugin) to make this function but it doesn't worked because some error with my Wordpress Theme. I installed DataTables succesfuly but I need a better sorting options so I discovered Natural Sorting. I think I am doing something wrong because it doesn't work.

Here is what I had put in my header:

[code] <!-- DataTables -->

@import "../media/css/demo_page.css";
@import "../media/css/demo_table.css";




$(document).ready(function() {
$('#example').dataTable();
} );

<!-- DataTables -->

<!-- NaturalSort -->


jQuery.fn.dataTableExt.oSort['natural-asc'] = function(a,b) {
return naturalSort(a,b);
};

jQuery.fn.dataTableExt.oSort['natural-desc'] = function(a,b) {
return naturalSort(a,b) * -1;
};

<!-- NaturalSort -->[/code]

I think I also need to put the class in my table but I don't know where to get the class name and where to put in my table. I need my entire table to be using Natural Sorting.

What am I doing wrong?

Replies

  • allanallan Posts: 61,669Questions: 1Answers: 10,096 Site admin
    Try moving the natural sorting code to before the DataTables initialisation, and also you need to specify the sType of the columns you want to sort as 'natural'. Example: http://datatables.net/examples/plug-ins/sorting_sType.html . You could also use aoColumnDefs with aTargets: [ '_all' ], to have all columns using natural sorting.

    Allan
  • alcacervealcacerve Posts: 2Questions: 0Answers: 0
    edited March 2011
    Hello, I think I am in the right way, but it is not sorting correctly.

    I am having this: US$10.90 < US$11.50 < US$5.00 < US$6.00

    But I need this: US$5.00 < US$6.00 < US$10.90 < US$11.50

    Here is my code:

    [code] <!-- NaturalSort -->


    jQuery.fn.dataTableExt.oSort['natural-asc'] = function(a,b) {
    return naturalSort(a,b);
    };

    jQuery.fn.dataTableExt.oSort['natural-desc'] = function(a,b) {
    return naturalSort(a,b) * -1;
    };

    <!-- NaturalSort -->

    <!-- DataTables -->

    @import "../media/css/demo_page.css";
    @import "../media/css/demo_table.css";




    $(document).ready(function() {
    $('#example').dataTable( {
    "aoColumnsDefs": [
    null,
    null,
    null,
    null,
    null,
    null,
    { "sType": "natural-desc" },
    ]
    } );
    } );

    <!-- DataTables -->[/code]

    *Instead of aoColumns I changed to aoColumnsDefs because reading the manual aTargets requests the last one.
  • allanallan Posts: 61,669Questions: 1Answers: 10,096 Site admin
    edited March 2011
    sType should be "natural" rather than "natural-desc" - which is a descending sort. DataTables will add the '-desc' or '-asc' automatically as needed.

    Allan
This discussion has been closed.