1

I am using Datatables for Sorting the ID column.

ID

MPT123

RST789

AZE190

How can I sort this based on Numeric Values I tried by doing

"columnDefs": [{
            targets: "datatable-nosort",
            orderable: false,
            bsort: false,
            sType: "numeric"
}]

But this didn't worked.

2
  • So you want to sort on the number, ignoring the letters before it? Are all IDs of the form AAA000? Commented Sep 2, 2016 at 15:04
  • Yes I want to sort on number Commented Sep 2, 2016 at 15:10

2 Answers 2

1

I got it working , posting the working code

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"formatted-num-pre": function ( a ) {       
    a = (a === "-" || a === "") ? 0 : a.replace( /[^\d\-\.]/g, "" );
    return parseFloat( a );
},

"formatted-num-asc": function ( a, b ) {
    return a - b;
},

"formatted-num-desc": function ( a, b ) {
    return b - a;
}
} );

And call the method as

{ "type": "formatted-num", targets: 0 }]
Sign up to request clarification or add additional context in comments.

1 Comment

Just FYI, there might be a simpler way using the render function and returning a parseInt of the number part of the string when type = 'sort'. Kudos on finding a solution on your own, though!
0

I'm using natural sorting JS and works great - https://datatables.net/plug-ins/sorting/natural#Example

Sort data with a mix of numbers and letters naturally. Data can often be a complicated mix of numbers and letters (file names are a common example) and sorting them in a natural manner is quite a difficult problem.

Fortunately a deal of work has already been done in this area by other authors - the following plug-in uses the naturalSort() function by Jim Palmer to provide natural sorting in DataTables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.