columns().order()

Apply multi-column ordering through the columns() API method.

The DataTables core library provides the ability to set the ordering via the column().order() method, but there is no plural equivalent. While multi-column ordering can be set using order() that method requires that column indexes be used.

This plug-in provides the plural columns().order() method so you can set multi-column ordering, while retaining the benefits of the columns() selector options.

Plug-in code

$.fn.dataTable.Api.register( 'columns().order()', function ( dir ) {
  return this.iterator( 'columns', function ( settings, columns ) {
    var a = [];
    
    for ( var i=0, ien=columns.length ; i<ien ; i++ ) {
      a.push( [ columns[i], $.isArray(dir) ? dir[i] : dir ] );
    }
    
    new $.fn.dataTable.Api( settings ).order( a );
  } );
} );

CDN

This plug-in is available on the DataTables CDN:

JS

Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!

Examples

// Apply multi-column sorting with a common direction
  table.columns( [ 1, 2 ] ).order( 'desc' ).draw();
// Multi-column sorting with individual direction for the columns
  table.columns( [ 1, 2 ] ).order( [ 'desc', 'asc' ] ).draw();
// Multi-column sorting based on a name selector
  table.columns( [ 'sign_up_date:name', 'user_name:name' ] ).order( 'desc' ).draw();