How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Change the Default Sort Order in DataTables
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Change the Default Sort Order in DataTables
Web Development

How to Change the Default Sort Order in DataTables

how7o
By how7o
Last updated: May 22, 2026
4 Min Read
DataTables default sort order — order: [[1, 'desc']] config
SHARE

To change the default sort order in DataTables, pass an order option in the initialization config. Each entry is [columnIndex, 'asc'|'desc']. For server-side processing, DataTables sends this initial sort to your backend as the first request — your server code reads order[0][column] and order[0][dir] and adds the matching ORDER BY.

Contents
  • TL;DR
  • Setting the initial sort
  • Server-side: what arrives at your backend
  • Disable sorting on specific columns
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 with DataTables 1.13+ / 2.x. Originally published 2022-11-20, rewritten and updated 2026-05-17.

TL;DR

$('#myTable').DataTable({
  processing:    true,
  serverSide:    true,
  serverMethod:  'post',
  ajax:          '/api/data',

  // Initial sort: column index 1, descending
  order: [[1, 'desc']],

  // Multiple columns: 1 desc, then 3 asc as tiebreaker
  // order: [[1, 'desc'], [3, 'asc']],

  // No initial sort — use whatever order the server returns
  // order: [],
});

Setting the initial sort

The order option takes an array of [index, direction] pairs. The column index is zero-based, matching the position of the <th> in the table header. Direction is 'asc' or 'desc'.

// Sort by the second column descending on initial load
$('#myTable').DataTable({
  order: [[1, 'desc']],
});
DataTables default sort order — order option, server request format, column-disable

Server-side: what arrives at your backend

With serverSide: true, DataTables sends the sort settings on every request — including the first one. The request payload contains:

order[0][column] = 1
order[0][dir]    = desc

# Multiple-column example
order[0][column] = 1
order[0][dir]    = desc
order[1][column] = 3
order[1][dir]    = asc

Your server reads these and appends the matching ORDER BY to the query. In a typical Laravel / PHP controller:

// Laravel example — receive the order array from the DataTables AJAX request
$orders = $request->input('order', []);
$columns = $request->input('columns', []);

foreach ($orders as $o) {
    $colIndex = (int) $o['column'];
    $dir      = $o['dir'] === 'desc' ? 'desc' : 'asc';
    $colName  = $columns[$colIndex]['data'] ?? null;
    if ($colName) {
        $query->orderBy($colName, $dir);
    }
}

Disable sorting on specific columns

$('#myTable').DataTable({
  order: [[1, 'desc']],
  columnDefs: [
    { targets: [0, 4], orderable: false },
  ],
});

Columns at index 0 and 4 get no sort indicator and no click handler. Common for action columns (Edit/Delete buttons) and index columns where sorting makes no sense.

Frequently asked questions

How do I sort by multiple columns?

Add more sub-arrays inside order. order: [[1, 'desc'], [3, 'asc']] sorts first by column index 1 descending, then breaks ties with column index 3 ascending. The user can later override this by clicking column headers — the order option only sets the initial sort.

How do I disable initial sorting entirely?

Pass an empty array: order: []. DataTables will load the data in the order returned by the server (server-side mode) or in DOM order (client-side mode), with no ORDER BY clause appended. Useful when your backend already returns the right order.

How do I disable sorting on a specific column?

Use the columnDefs option with orderable: false. Example: columnDefs: [{ targets: [0, 4], orderable: false }] disables sorting on columns 0 and 4. The header gets no click handler and no sort indicator.

Does order affect server-side processing?

Yes — DataTables sends the initial sort to your server as order[0][column]=1&order[0][dir]=desc in the very first request. Your backend should pick this up and add the matching ORDER BY to the query. Without the order option, DataTables defaults to [[0, 'asc']], which is why most server-side tables sort by the first column on initial load even when you didn’t intend it.

Related guides

  • How to Add the Required Attribute to Input Fields with jQuery
  • How to Break Out of a jQuery .each() Loop
  • How to Auto-focus a Select2 Dropdown on Page Load

References

DataTables order option: datatables.net/reference/option/order. Server-side processing protocol: datatables.net/manual/server-side.

TAGGED:datatablesJavaScriptjQuery

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article CSS checkbox background color — accent-color one-liner and hide-and-replace fallback How to Change a Checkbox Background Color with CSS
Next Article Change SSH port on Linux — firewall, SELinux, sshd_config, systemctl restart How to Change the Default SSH Port on Linux
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Run Laravel queue workers with Supervisor
How to Run Laravel Queue Workers in Production with Supervisor
May 23, 2026
Nginx as a reverse proxy for a Node.js app on Ubuntu
How to Set Up Nginx as a Reverse Proxy for Node.js on Ubuntu
May 23, 2026
Install and configure Redis on Ubuntu for Laravel and WordPress
How to Install and Configure Redis on Ubuntu (for Laravel & WordPress)
May 23, 2026
Harden a fresh Ubuntu VPS with UFW, Fail2Ban, and SSH key auth
How to Harden a Fresh Ubuntu VPS: UFW + Fail2Ban + SSH Key Auth
May 23, 2026
Set up Let's Encrypt SSL with Certbot on Ubuntu
How to Set Up Let’s Encrypt SSL with Certbot on Ubuntu (Apache & Nginx)
May 23, 2026

You Might Also Like

WooCommerce auto add to cart on visit — template_redirect hook and cart dedup
Web Development

How to Automatically Add a Product to Cart on Visit in WooCommerce

8 Min Read
Laravel DataTables custom column search — filterColumn callback handles the search SQL
Web Development

How to Search Custom or Composite Columns in Laravel DataTables

8 Min Read
Laravel Eloquent records today — Carbon today helper and whereDate illustration
Web Development

How to Get Records Created Today in Laravel

6 Min Read
Create a folder in PHP if it does not already exist
Web Development

How to Create a Folder If It Does Not Exist in PHP

5 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?