how to ignore a specific string or 'N/A' for sorting.

how to ignore a specific string or 'N/A' for sorting.

siddharthawebsiddharthaweb Posts: 2Questions: 2Answers: 0

if type is 'percent' and in case of 'N/A' , these row always should appear at the end .

$(document).ready(function() {

$.extend( true, $.fn.dataTable.defaults, {
    "bPaginate":       false, //hide pagination
    "bFilter":         false, //hide Search bar
    "bInfo":           false, // hide showing entries
    "bAutoWidth":      false,
    "fixedColumns":    true
} );

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

    "percent-asc": function ( a, b ) {
        if (isNaN(a) && isNaN(b)) return ((a < b) ? 1 : ((a > b) ? -1 : 0));

        if (isNaN(a)) return 1;
        if (isNaN(b)) return -1;

        a = parseFloat( a );
        b = parseFloat( b );
        return parseFloat((a < b) ? -1 : ((a > b) ? 1 : 0));
    },

    "percent-desc": function ( a, b ) {
        if (isNaN(a) && isNaN(b)) return ((a < b) ? 1 : ((a > b) ? -1 : 0));

        if (isNaN(a)) return 1;
        if (isNaN(b)) return -1;

        a = parseFloat( a );
        b = parseFloat( b );
        return parseFloat((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
} );


$('#HTML-ID').DataTable( {
    "order":           [[ 6, "desc" ]],
    "columnDefs":      [{ type: 'percent', targets: 6 },{ "type": "percent", "targets": 4 }]
} );

} );

Answers

  • allanallan Posts: 62,315Questions: 1Answers: 10,225 Site admin

    You've made a good start by using a custom sorting plug-in. Now you just need to modify it so that N/A comes at the bottom of the table. Using return -Infinity from the -pre function for such data should do that.

    Allan

  • justinTjustinT Posts: 2Questions: 1Answers: 0

    @siddharthaweb ... Could you post your updated code please? I'm having trouble with this same problem and am not exactly sure what @allan is meaning with his comment.

    Thanks,
    @justinT

This discussion has been closed.