# Table Layouts - Column Sorting

Canonical page: https://www.adaptabletools.com/docs/handbook-layouts-table-sorting

- The Table Layout fully supports sorting, including Custom Sorts

[Layouts](https://www.adaptabletools.com/docs/handbook-layouts/index.md) · [Table Layouts](https://www.adaptabletools.com/docs/handbook-layouts-table/index.md) · [All Table Layout Topics](https://www.adaptabletools.com/docs/handbook-layouts-table/index.md#table-layout-topics)

Very often, Table Layouts will contain **Column Sorting information**.

The `ColumnSorts` property allows developers to define which columns will be sorted when the Layout loads.

Column Sorting defined in a Layout **take precedence** over sorting set in `columnDefs` in GridOptions

### Defining a Sorted Table Layout

Layouts can include Column Sorting definitions

The `ColumnSorts` property (of type [`ColumnSort`](https://www.adaptabletools.com/docs/reference/columnsort.md)) is an array containing 2 properties:

- a `ColumnId` (string)
- the `SortOrder` - either `Asc` or `Desc`

The Columns will be sorted in the order provided (but don't need to be visible).

If the Column has a [Custom Sort](https://www.adaptabletools.com/docs/handbook-custom-sorting/index.md), that will be used to sort the Column.

```ts [[1, 11, "ColumnSorts"]]
// Provide 2 Sorted Columns: Language & License (the latter not visible)
// Language: sorted in descending order
// License: sorted in ascending order (and not initially visible in the Grid)
const initialState: InitialState = {
  Layout: {
    CurrentLayout: 'Standard Layout',
    Layouts: [
      {
        Name: 'Standard Layout',
        TableColumns: ['name', 'language', 'github_stars', 'open_pr_count'],
        ColumnSorts: [
          {
            ColumnId: 'language',
            SortOrder: 'Desc',
          },
          {
            ColumnId: 'license',
            SortOrder: 'Asc',
          },
        ],
      },
    ],
  },
};
```

## Applying Sorting

Sorting can be applied at run-time in one of 2 ways:

- in the `Sort` step of the [Table Layout UI Wizard](https://www.adaptabletools.com/docs/handbook-layouts-wizard-table/index.md)
- clicking the Header of a Column marked as sortable by AG Grid

If [multiple column sorting](https://www.ag-grid.com/javascript-data-grid/row-sorting/#multi-column-sorting) is enabled, then hold Shift while clicking a second column header to add a secondary sort

Column Sorts defined at run-time are automatically persisted in the Layout State.

## Custom Sorts

AdapTable provides a [Custom sorting](https://www.adaptabletools.com/docs/handbook-custom-sorting/index.md) module.

Custom Sorts are bespoke sort orders which enable columns to be supported in a non-standard way.

Columns that have a Custom Sort are included in ColumnSorts in the normal way, and AdapTable will perform the bespoke sort order automatically when sorting is applied.

**Example: Table Layouts: Sorting**

Creating Table Layouts with Column Sorting

- This example contains a `Sorted Layout`  with sorting on 2 columns:
  - `Licence` (Ascending and also using a [Custom Sort](https://www.adaptabletools.com/docs/handbook-custom-sorting/index.md))
  - `Name` (Descending)

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Table Layouts Sorting',
  initialState: {
    Dashboard: {
      ModuleButtons: ['Layout'],
      Tabs: [
        {
          Name: 'Default',
          Toolbars: ['Layout', 'GridFilter'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    CustomSort: {
      CustomSorts: [
        {
          Name: 'customSort-license',
          ColumnId: 'license',
          SortedValues: ['Other'],
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Sorted',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'github_watchers',
            'description',
            'open_issues_count',
            'closed_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'has_projects',
            'has_pages',
            'week_issue_change',
          ],
          Name: 'Sorted',
          ColumnSorts: [
            {
              ColumnId: 'license',
              SortOrder: 'Asc',
            },
            {
              ColumnId: 'name',
              SortOrder: 'Desc',
            },
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## Invisible Columns

AdapTable allows the Grid to be sorted by Columns which are not visible.

**Example: Sorting on Invisible Columns**

Sorting the Grid using Hidden Columns

- This example includes 2 Layouts each of which includes Column Sorts:
  1. `Unsorted Layout` - grid is not sorted so the data is displayed in the order it appears in the data source
  2. `Hidden Sorted Layout` - sorted by `Name` in Descending Order
- Note that in the Hidden Sorted Layout the Name column is not visible; the grid still sorts as requested

- Switch between the 2 Layouts to see how the Sorting is applied

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Sorting on Hidden Column',
  initialState: {
    Dashboard: {
      Tabs: [
        {
          Name: 'Demo',
          Toolbars: ['Layout'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Unsorted Layout',
      Layouts: [
        {
          Name: 'Unsorted Layout',
          TableColumns: [
            'name',
            'license',
            'language',
            'week_issue_change',
            'github_stars',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'open_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'has_projects',
            'has_pages',
          ],
          AutoSizeColumns: true,
        },
        {
          Name: 'Hidden Sorted Layout',
          TableColumns: [
            'license',
            'language',
            'week_issue_change',
            'github_stars',
            'created_at',
            'has_wiki',
            'updated_at',
            'pushed_at',
            'open_issues_count',
            'open_pr_count',
            'closed_pr_count',
            'has_projects',
            'has_pages',
          ],
          ColumnSorts: [
            {
              ColumnId: 'name',
              SortOrder: 'Desc',
            },
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## Autogenerated Columns

AdapTable supports custom Sorting of Columns automatically created by AG Grid.

This is typically done by adding the autogenerated ColumnId to the `ColumnSorts` property in the Layout.

- See [Sorting Row Groups](https://www.adaptabletools.com/docs/handbook-grouping-rows/index.md) for instructions on sorting Row Grouped Columns
- See [Sorting Tree Data Grid](https://www.adaptabletools.com/docs/handbook-tree-data-grid/index.md#sorting) for instructions on sorting Tree Data key columns

### FAQ

**How do I sort grouped Columns?**
AdapTable will automatically sort any Row-Grouped Columns when the Column Header is clicked. This will take into account any [Custom Sorts](https://www.adaptabletools.com/docs/handbook-custom-sorting/index.md) which have been configured.

**Not all the values we want to include in the sort are currently in the grid, so they don't appear in the dropdown. Can we add items by hand?**
That is not possible in the wizard. In previous versions of AdapTable where it was allowed, users ended up with multiple differently-spelled variations of the same item. The wizard therefore only offers **distinct values currently present** in that column — you cannot type arbitrary strings. Define a list-based [custom sort](https://www.adaptabletools.com/docs/handbook-custom-sorting/index.md) in [Initial Adaptable State](https://www.adaptabletools.com/docs/dev-guide-adaptable-state-initial-state/index.md) when you need an order that includes values not shown in the grid.

**Can we run the Custom Sort when pivoting?**
Yes - any column that has a Custom Sort will have it automatically applied if the column is part of a pivot view.

**Why are Row-Grouped Columns always sorted last even if they are positioned elsewhere in the Layout?**
That is standard AG Grid behaviour. The Column being row-grouped is always sorted last, regardless of what sort order is given to the Grid.
