# Sorting

> Enable column sorting and choose an initial order.

Source: https://docs.inertiax.dev/table/guide/sorting/

Call `sortable()` to make a column sortable:

```php
use InertiaX\Table\Columns\TextColumn;

TextColumn::make('name')->sortable();
```

## Set the initial order

On a table with a sortable `name` column:

```php
UsersTable::make('users')->data($users)->initialSort('name');
```

Prefix the key with `-` for descending order: `initialSort('-name')`.

Multiple sorting is enabled by default. Supply an ordered list when more than one column should participate; each listed column must be sortable:

```php
UsersTable::make('users')->data($users)->initialSort([
    ['key' => 'role', 'direction' => 'asc'],
    ['key' => 'name', 'direction' => 'desc'],
]);
```

Use `multiSort(false)` to restrict sorting to one column.

## Keep a fixed query order

If the query already owns the order, disable interactive sorting:

```php
UsersTable::make('users')
    ->data($users->latest())
    ->sorting(false);
```

For interactive sorting, supply a query without an existing order. InertiaX does not remove application query ordering before adding the table’s sort.

Sorting resets pagination to page 1. See [Table API](https://docs.inertiax.dev/table/reference/table/#sorting) for tie-breakers and validation, or [Custom query behavior](https://docs.inertiax.dev/table/guide/query-callbacks/) to sort a different field.

## Sort links and custom fields

See [URL state](https://docs.inertiax.dev/table/guide/url-state/#sort-links) for shareable sort links and accepted formats.

Use [`relationship()`](https://docs.inertiax.dev/table/reference/column/#related-scalar-fields) with `sortable()` to sort by a persisted field through a standard, single-hop `BelongsTo` relation. For standard, single-hop `HasMany` aggregates, use [`count()` or `sum()`](https://docs.inertiax.dev/table/reference/column/#relationship-counts-and-sums) with `sortable()`.

For other query shapes, project the displayed value with a subquery and declare `sortUsing()` against the relationship. A displayed alias alone does not define relationship sorting or search behavior. See [Custom query behavior](https://docs.inertiax.dev/table/guide/query-callbacks/) and the [callback contexts](https://docs.inertiax.dev/table/reference/callbacks/).
