Search code examples
javascriptphpdatatablecolumnsorting

how to sort data table rank column rank wise which contains value 'Absent' also


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,...


Solution

  • 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