As you would expect with a desktop application, DataTables allows you to sort by multiple columns at the same time. This multiple sorting mechanism is always active if the bSort initialiser is true (it is by default) and the end user can activate it by 'shift' clicking on the column they want to add to the sort. You can also pass in an array of information using the aaSorting initialiser, as I have done in the example below there the first column is sorted as the primary column and the second one then used if the elements in the first column match. As many columns as you wish can be added to the sort.
DataTables also provides a method to add your own sorting functions, to extend those built into DataTables. This can be very useful if you wish to sort on data formats such as currency and non-Javascript standard date formats (this natural sort algorithm is a popular useage). This is achieved by extending the jQuery.fn.dataTableExt object with ascending and descending sort functions. In the example below I've added case sensitive sorting functions.
Rendering engine | Browser | Platform(s) | Engine version | CSS grade |
---|---|---|---|---|
Rendering engine | Browser | Platform(s) | Engine version | CSS grade |
Gecko | Camino 1.0 | OSX.2+ | 1.8 | A |
Gecko | Camino 1.5 | OSX.3+ | 1.8 | A |
Gecko | Epiphany 2.20 | Gnome | 1.8 | A |
Gecko | Firefox 1.0 | Win 98+ / OSX.2+ | 1.7 | A |
Gecko | Firefox 1.5 | Win 98+ / OSX.2+ | 1.8 | A |
Gecko | Firefox 2.0 | Win 98+ / OSX.2+ | 1.8 | A |
Gecko | Firefox 3.0 | Win 2k+ / OSX.3+ | 1.9 | A |
Gecko | Mozilla 1.0 | Win 95+ / OSX.1+ | 1 | A |
Gecko | Mozilla 1.1 | Win 95+ / OSX.1+ | 1.1 | A |
Gecko | Mozilla 1.2 | Win 95+ / OSX.1+ | 1.2 | A |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /* Define two custom functions (asc and desc) for string sorting */ jQuery.fn.dataTableExt.oSort[ 'string-case-asc' ] = function (x,y) { return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }; jQuery.fn.dataTableExt.oSort[ 'string-case-desc' ] = function (x,y) { return ((x < y) ? 1 : ((x > y) ? -1 : 0)); }; $(document).ready( function () { /* Build the DataTable with third column using our custom sort functions */ $( '#example' ).dataTable( { "aaSorting" : [ [0, 'asc' ], [1, 'asc' ] ], "aoColumns" : [ null , null , { "sType" : 'string-case' }, null , null ] } ); } ); |