DataTables logo DataTables

Sorting dilemma - none of the column types seem to work
  • I have a column with data like:

    50
    100 of 100
    10 of 75
    55 of 100
    78
    5 of 20
    65 of 250
    432
    ...

    I want to order them by the first number, so:
    5 of 20
    10 of 75
    50
    55 of 100
    65 of 250
    78
    100 of 100
    432

    For aoColumns sType, I've tried:
    - string-case
    - null
    - html
    - string
    - numeric

    But nothing seems to sort properly. I'll typically get the 10 and 100 of 100 rows together since they both start with "1". And numeric likely doesn't work because I have strings in the data.

    Any ideas?

    Thanks!
  • Hi,

    I am no master of DataTables (just started using it yesterday) but I think your solution is to write your own sorting comparator.

    Custom sorting in data tables:
    http://datatables.net/development/sorting#type_based

    Your comparator can take the left and right values, split them both on " " (space) and then compare the first element returned from split numerically. A comparison function similar to

    function compare(x,y) {
        var x1 = Number(x.split(" ")[0]);
        var y1 = Number(y.split(" ")[0]);
        return ((x1 < y1) ? -1 : ((x1 > y1) ?  1 : 0));
    }
    
  • You rock - that worked - thanks so much!

    jQuery.fn.dataTableExt.oSort['submissions-asc'] = function(x,y) {
    		var x1 = Number(x.split(" ")[0]);
    		var y1 = Number(y.split(" ")[0]);
    		return ((x1 < y1) ? -1 : ((x1 > y1) ? 1 : 0));
    }
    jQuery.fn.dataTableExt.oSort['submissions-desc'] = function(x,y) {
    		var x1 = Number(x.split(" ")[0]);
    		var y1 = Number(y.split(" ")[0]);
    		return ((x1 < y1) ? 1 : ((x1 > y1) ? -1 : 0));
    }
This discussion has been closed.
All Discussions

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Support

Get useful and friendly help straight from the source.