0

my datatable containes a column Rank. it includes rank of students starting from 1 and if a student is absent rank is defined as 'Absent'. The problem is when sorting this column it comes like 1,10,11,12,...,2,20,21,..,Absent,Absent,..

my data table initialisation is

$(document).ready( function () { 

        var  oTable = $('#filtertableobj').dataTable({
            "iDisplayLength": 500,"aLengthMenu": [[100, 200, 500, 1000], [100, 200, 500, 1000]],

            /*BEGIN Fixing the index row so they are not sorted -r2ros */
            "fnDrawCallback": function ( oSettings ) {
                var that = this;            
                /* Need to redo the counters if filtered or sorted */            
                if ( oSettings.bSorted || oSettings.bFiltered )            
                {              
                    this.$('td:first-child', {"filter":"applied"}).each( function (i) 
                    {                    
                        that.fnUpdate( i+1, this.parentNode, 0, false, false );                
                    } );            
                }        
            }, 

            "aoColumnDefs": [{ "bSortable": false, "aTargets": [ 0  ] } ],


        });

   });

How to sort like 1,2,3,...,10,11,12,..,Absent,Absent,...

anjana
  • 65
  • 1
  • 8

1 Answers1

0

What you're looking for is called a "natural sort". If you search that in SO you'll find your answer. Try this Sort Array Elements (string with numbers), natural sort

Community
  • 1
  • 1
Cyph
  • 623
  • 1
  • 7
  • 25
  • How to include this in data table? Datatable takes that column as string,how to define custom sorting there? – anjana Nov 21 '15 at 04:29
  • there are some existing plugins for datatables that do natural sorting, see https://www.datatables.net/plug-ins/sorting/natural – Cyph Nov 21 '15 at 04:34
  • thanks.it works when i add the plugin.and by giving "aoColumns": [ null, null, null, null, null, { "sType": "numeric" }, { "sType": "natural" }, null ]. But in this the 3rd column is Name of students ,it doesn't work by giving type as string – anjana Nov 21 '15 at 04:52