Need help desc order is not working in sorting

Need help desc order is not working in sorting

kalpesh.vyas26kalpesh.vyas26 Posts: 10Questions: 2Answers: 0

I tried following code to make numbers like '100-1001', '100-11' to short properly but descending code is not working at all. Please help

j$.fn.dataTable.ext.type.order['intParse-desc'] = function ( x, y ) {
var a= parseInt(x.replace("-", ""));
var b= parseInt(y.replace("-", ""));
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
};
j$.fn.dataTable.ext.type.order['intParse-asc'] = function ( x, y ) {
var a= parseInt(x.replace("-", ""));
var b= parseInt(y.replace("-", ""));
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
};

ticketTable = j$('[id$="ticketTable"]').DataTable({
columnDefs: [{ type: 'intParse', targets: 0 }]
});

Replies

  • kalpesh.vyas26kalpesh.vyas26 Posts: 10Questions: 2Answers: 0

    Neither it is working following way
    j$.extend( jQuery.fn.dataTableExt.oSort, {
    "intParse-asc": function ( x, y ) {
    a= parseInt(x.replace("-", ""));
    b= parseInt(y.replace("-", ""));
    return -((a < b) ? -1 : ((a > b) ? 1 : 0));
    },
    "intParse-desc": function ( x, y ) {
    a= parseInt(x.replace("-", ""));
    b= parseInt(y.replace("-", ""));
    return -((a < b) ? 1 : ((a > b) ? -1 : 0));
    }

            });
    
  • bindridbindrid Posts: 364Questions: 0Answers: 44

    Since it is not actually a number, why do you not just sort it as a string?

  • kalpesh.vyas26kalpesh.vyas26 Posts: 10Questions: 2Answers: 0

    No string will not work as client is looking it as number.
    Like
    100-11
    100-20
    100-100
    100-102

    It should short like this

  • allanallan Posts: 40,558Questions: 1Answers: 4,957 Site admin

    You'd need to write an ordering plug-in that will sort your data as you require since that isn't strictly numeric only data.

    Another option might be to use the natural sorting plug-in.

    Allan

  • kalpesh.vyas26kalpesh.vyas26 Posts: 10Questions: 2Answers: 0

    Thanks @allan it worked for me natural sorting plug-in

Sign In or Register to comment.