JSFiddle - React, Tailwind, and code Playground

by osushil

HTML

<link rel="stylesheet" href="https://cdn.datatables.net/1.10.2/css/jquery.dataTables.css">
<script src="https://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
<table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th data-index="0">Name</th>
            <th data-index="1">Position</th>
            <th data-index="2">Office</th>
            <th data-index="3">Age</th>
            <th data-index="4">Start date</th>
            <th data-index="5">Salary</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>$320,800</td>
        </tr>
        <tr>
            <td>Garrett Winters</td>
            <td>Accountant</td>
            <td>Tokyo</td>
            <td>63</td>
            <td>2011/07/25</td>
            <td>$170,750</td>
        </tr>
        <tr>
            <td>Ashton Cox</td>
            <td>Junior Technical Author</td>
            <td>San Francisco</td>
            <td>66</td>
            <td>2009/01/12</td>
            <td>$86,000</td>
        </tr>
        <tr>
            <td>Cedric Kelly</td>
            <td>Senior Javascript Developer</td>
            <td>Edinburgh</td>
            <td>22</td>
            <td>2012/03/29</td>
            <td>$433,060</td>
        </tr>
        
        <tr>
            <td></td>
            <td>Junior Technical Author</td>
            <td>San Francisco</td>
            <td>66</td>
            <td>2009/01/12</td>
            <td>$86,000</td>
        </tr>
        <tr>
            <td></td>
            <td>Senior Javascript Developer</td>
            <td>Edinburgh</td>
            <td>22</td>
            <td>2012/03/29</td>
            <td>$433,060</td>
        </tr>
        
    </tbody>
</table>

JavaScript

jQuery.extend( jQuery.fn.dataTableExt.oSort, {
    "non-empty-string-asc": function (str1, str2) {
        if(str1 == "")
            return 1;
        if(str2 == "")
            return -1;
        return ((str1 < str2) ? -1 : ((str1 > str2) ? 1 : 0));
    },
 
    "non-empty-string-desc": function (str1, str2) {
        if(str1 == "")
            return -1;
        if(str2 == "")
            return 1;
        return ((str1 < str2) ? 1 : ((str1 > str2) ? -1 : 0));
    }
} );


$('#example').dataTable({
    columnDefs: [
       {type: 'non-empty-string', targets: [0,5]} // define 'name' column as non-empty-string type
    ]
});


$('#example').on( 'click', 'th', function(){
		    var idx = $(this).data("index");
		    var sortOrders = $('#example').dataTable().api().order();
		    var secondary_column = 5;
		  
			if(sortOrders[0].indexOf(idx)>-1  && sortOrders[0][1]==='asc'){
				$('#example').dataTable().api().order( [ idx , 'asc' ],[ secondary_column, 'asc' ]  ).draw();
			}else{
				$('#example').dataTable().api().order( [ idx , 'desc' ],[ secondary_column, 'desc' ] ).draw();
			}
		});