James
James

Reputation: 720

Custom Ordering Jquery DataTables

I am trying to create a custom ordering system to sort the rows with a special alpha numberic format

The data in the column is a distance call out such as "1.79 mi" , "10.21 mi" or "9.21 mi"

My issue is that when I sort desc it will put the 9.21 after the 10.21.

I am using the following code.

aaSorting = [[3,'desc']];

I am assuming I have to create special definitions using aoColumnDefs and sType but I cant seem to figure them out.

Upvotes: 0

Views: 110

Answers (1)

marathonman
marathonman

Reputation: 452

you can add your own plugin for sorting.

check other examples https://datatables.net/plug-ins/sorting/

jQuery.extend(jQuery.fn.dataTableExt.oSort, {
    "distance-pre": function (a) {
        var x = (a == "-") ? 0 : a.replace(/mi/, "");
        return parseFloat(x);
    },

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

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

Usage

type: 'distance'

Upvotes: 1

Related Questions