# Sorting Pivot Layouts

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

- Pivot Layouts can use the same sorting as Table Layouts
- Both Aggregation Columns and Pivot Result Columns can be sorted
- Custom sorting can be applied to Aggregation Columns in Pivot Layouts

[Layouts](https://www.adaptabletools.com/docs/handbook-layouts/index.md) · [Pivot Layouts](https://www.adaptabletools.com/docs/handbook-layouts-pivot/index.md) · [All Pivot Layout Topics](https://www.adaptabletools.com/docs/handbook-layouts-pivot/index.md#pivot-layout-topics).
Table layout sorts — [Table Layouts — Column Sorting](https://www.adaptabletools.com/docs/handbook-layouts-table-sorting/index.md).

All Columns in the Pivot Layout can be sorted - and the sorted column persisted to AdapTable State.

## Aggregation Columns 

It is straightforward to set Sorting for standalone Pivot Aggregation Columns.

This is done using the standard `ColumnSorts` property in the Layout.

- Sorting a Pivot Aggregation Column will not sort that column when there are Pivot Result Columns
- Instead each Pivot Result Column will need to be sorted individually (see below)

**Example: Pivot Sorting: Aggregation Columns**

Setting & Saving Column Sorting for Pivot Aggregation Columns

 

- In this example we set Column Sorting for the 2 Pivot Aggregation Columns - `Github Stars` and `Github Watchers`
- We have not provided any PivotColumns - so there are no dynamic Pivot Result Columns created 

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: 'Pivot Aggregation Columns: Sorting',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Pivot Layout',
      Layouts: [
        {
          Name: 'Pivot Layout',
          PivotColumns: [],
          PivotGroupedColumns: ['license'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          ColumnSorts: [
            {
              ColumnId: 'github_stars',
              SortOrder: 'Asc',
            },
            {
              ColumnId: 'github_watchers',
              SortOrder: 'Desc',
            },
          ],
        },
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'popularity',
            'license',
            'open_issues_count',
            'created_at',
            'has_wiki',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## Pivot Result Columns

It is also possible to sort [Pivot Result Columns](https://www.adaptabletools.com/docs/handbook-layouts-pivot-result-columns/index.md) and save this configuration in the [Layout](https://www.adaptabletools.com/docs/handbook-layouts/index.md).

This is also done via the `ColumnSorts` property, by using the AG Grid generated colId for the Pivot Result Column.

**Example: Pivot Sorting: Pivot Result Columns**

Setting & Saving Column Sorting for Pivot Result Columns

 

- In this example we set Column Sorting for 2 Pivot Result Columns in the Layout Definition (both using AG Grid's autogenerated `ColumnId`)
- `pivot_language_HTML_github_stars` and `pivot_language_JavaScript_github_watchers` are sorted ascendingly

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: 'Pivot Result Columns: Sorting',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Pivot Layout',
      Layouts: [
        {
          Name: 'Pivot Layout',
          PivotColumns: ['language'],
          PivotGroupedColumns: ['license'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          ColumnSorts: [
            {
              ColumnId: 'pivot_language_HTML_github_stars',
              SortOrder: 'Asc',
            },
            {
              ColumnId: 'pivot_language_JavaScript_github_watchers',
              SortOrder: 'Asc',
            },
          ],
        },
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'popularity',
            'license',
            'open_issues_count',
            'created_at',
            'has_wiki',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

## Custom Sorts

AdapTable allow for bespoke sorting via [Custom Sort](https://www.adaptabletools.com/docs/handbook-custom-sorting/index.md).

This is also available in Pivot Layouts, but only to Aggregation Columns (i.e. not to Pivot Result Columns).

**Example: Pivot Sorting: Custom Sorts**

Using Custom Sorts in Pivot Layout

 

- In this example we provide a, slightly random and pointless, Custom Sort on the `Github Stars` Column - we sort in this order: 138066, 9752, 867801
- This Custom Sort is applied in Column Sort that we apply to that Column

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: 'Pivot Custom Sorting',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    CustomSort: {
      CustomSorts: [
        {
          Name: 'customSort-github_stars',
          ColumnId: 'github_stars',
          SortedValues: [138066, 9752, 867801],
        },
        {
          Name: 'customSort-pivot_language_JavaScript_github_watchers',
          ColumnId: 'pivot_language_JavaScript_github_watchers',
          SortedValues: [21751, 204, 3375],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Pivot Agg Layout',
      Layouts: [
        {
          Name: 'Pivot Agg Layout',
          PivotColumns: [],
          PivotGroupedColumns: ['license'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          ColumnSorts: [
            {
              ColumnId: 'github_stars',
              SortOrder: 'Asc',
            },
          ],
        },
        {
          Name: 'Pivot Cols Layout',
          PivotColumns: ['language'],
          PivotGroupedColumns: ['license'],
          SuppressAggFuncInHeader: true,
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'sum',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          ColumnSorts: [
            {
              ColumnId: 'pivot_language_JavaScript_github_watchers',
              SortOrder: 'Asc',
            },
          ],
        },
      ],
    },
  },
};
```
