# Sorting Technical Reference

Canonical page: https://www.adaptabletools.com/docs/technical-reference-custom-sorting

## Custom Sort State

| Property | Type | Description |
| --- | --- | --- |
| [CustomSorts](https://www.adaptabletools.com/docs/reference/customsortstate.md#customsorts) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md)`[]` | Custom Sorts to apply. One column uses the first Custom Sort whose `ColumnId` matches. |

### Custom Sort

| Property | Type | Description |
| --- | --- | --- |
| [ColumnId](https://www.adaptabletools.com/docs/reference/customsort.md#columnid) | `string` | Id of Column on which Custom Sort will be applied |
| [Name](https://www.adaptabletools.com/docs/reference/customsort.md#name) | `string` | Name of the Custom Sort definition |
| [SortedValues](https://www.adaptabletools.com/docs/reference/customsort.md#sortedvalues) | `(string \| number)[]` | Sort order. Listed values come first, in this order. Any other value sorts after them. Dates are stored as ISO strings (`yyyy-MM-dd`). |
| [IsReadOnly](https://www.adaptabletools.com/docs/reference/customsort.md#isreadonly) | `boolean` | Sets Entity to ReadOnly (overwriting a Strategy Entitlement of &#39;Full&#39;) |
| [IsSuspended](https://www.adaptabletools.com/docs/reference/customsort.md#issuspended) | `boolean` | Suspends (i.e. turns off) an Adaptable Object |

-----------

## Custom Sort Options

| Property | Type | Description |
| --- | --- | --- |
| [customSortComparers](https://www.adaptabletools.com/docs/reference/customsortoptions.md#customsortcomparers) | [`ColumnValuesComparer`](https://www.adaptabletools.com/docs/reference/columnvaluescomparer.md)`<TData>[]` | CustomSort column comparer functions |

`customSortComparers` entries are a [`ColumnValuesComparer`](https://www.adaptabletools.com/docs/reference/columnvaluescomparer.md):

| Property | Type | Description |
| --- | --- | --- |
| [comparer](https://www.adaptabletools.com/docs/reference/columnvaluescomparer.md#comparer) | [`AdaptableComparerFunction`](https://www.adaptabletools.com/docs/reference/adaptablecomparerfunction.md)`<TData>` | Comparer function to use |
| [name](https://www.adaptabletools.com/docs/reference/columnvaluescomparer.md#name) | `string` | Name of the CustomSort Comparer |
| [scope](https://www.adaptabletools.com/docs/reference/columnvaluescomparer.md#scope) | [`ColumnScope`](https://www.adaptabletools.com/docs/reference/columnscope.md) | Column for which to compare values |

-----------

## Custom Sort API

| Method | Returns | Description |
| --- | --- | --- |
| [addCustomSort(customSort)](https://www.adaptabletools.com/docs/reference/customsortapi.md#addcustomsort) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md) | Adds a Custom Sort to Custom Sort collection in Adaptable State |
| [createCustomSort(params)](https://www.adaptabletools.com/docs/reference/customsortapi.md#createcustomsort) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md) | Creates new Custom Sort based on given values |
| [deleteCustomSort(columnId)](https://www.adaptabletools.com/docs/reference/customsortapi.md#deletecustomsort) | `void` | Removes Custom Sort for a given ColumnId |
| [editCustomSort(customSort)](https://www.adaptabletools.com/docs/reference/customsortapi.md#editcustomsort) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md) | Edits a Custom Sort |
| [editCustomSortValues(columnId, values)](https://www.adaptabletools.com/docs/reference/customsortapi.md#editcustomsortvalues) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md) | Updates existing Custom Sort with new set of Sorted Values |
| [getActiveCustomSorts()](https://www.adaptabletools.com/docs/reference/customsortapi.md#getactivecustomsorts) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md)`[]` | Retrieves all Custom Sorts that are active (not-suspended) in Adaptable State |
| [getCustomSortById(id)](https://www.adaptabletools.com/docs/reference/customsortapi.md#getcustomsortbyid) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md) | Retrieves Custom Sort by the technical ID (from `CustomSortState`) |
| [getCustomSortByName(name)](https://www.adaptabletools.com/docs/reference/customsortapi.md#getcustomsortbyname) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md)` \| undefined` | Retrieves a Custom Sort by its Name |
| [getCustomSortForColumn(columnId)](https://www.adaptabletools.com/docs/reference/customsortapi.md#getcustomsortforcolumn) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md)` \| undefined` | Retrieves Custom Sort from Adaptable State for Column with given ColumnId |
| [getCustomSorts()](https://www.adaptabletools.com/docs/reference/customsortapi.md#getcustomsorts) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md)`[]` | Retrieves all Custom Sorts in Adaptable State |
| [getCustomSortState()](https://www.adaptabletools.com/docs/reference/customsortapi.md#getcustomsortstate) | [`CustomSortState`](https://www.adaptabletools.com/docs/reference/customsortstate.md) | Retrieves Custom Sort section from Adaptable State |
| [getLiveCustomSortComparers()](https://www.adaptabletools.com/docs/reference/customsortapi.md#getlivecustomsortcomparers) | [`ColumnValuesComparer`](https://www.adaptabletools.com/docs/reference/columnvaluescomparer.md)`[]` | Returns all Custom Sort Comparers in Custom Sort Options that are currently applied |
| [getLiveCustomSorts()](https://www.adaptabletools.com/docs/reference/customsortapi.md#getlivecustomsorts) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md)`[]` | Retrieves all Custom Sorts in Adaptable State that are currently applied |
| [getSuspendedCustomSorts()](https://www.adaptabletools.com/docs/reference/customsortapi.md#getsuspendedcustomsorts) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md)`[]` | Retrieves all Custom Sorts that are suspended in Adaptable State |
| [openCustomSortSettingsPanel()](https://www.adaptabletools.com/docs/reference/customsortapi.md#opencustomsortsettingspanel) | `void` | Opens Settings Panel with Custom Sort section selected and visible |
| [suspendAllCustomSort()](https://www.adaptabletools.com/docs/reference/customsortapi.md#suspendallcustomsort) | `void` | Suspends all Custom Sorts |
| [suspendCustomSort(customSort)](https://www.adaptabletools.com/docs/reference/customsortapi.md#suspendcustomsort) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md) | Suspends Custom Sort |
| [unSuspendAllCustomSort()](https://www.adaptabletools.com/docs/reference/customsortapi.md#unsuspendallcustomsort) | `void` | Activates all suspended Custom Sort |
| [unSuspendCustomSort(customSort)](https://www.adaptabletools.com/docs/reference/customsortapi.md#unsuspendcustomsort) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md) | Un-suspends or activates a suspended Custom Sort |

-----------

## Grid Sorted Event

AdapTable fires the Grid Sorted Event whenever any column in AdapTable is sorted.

It provides information on what is being sorted in AdapTable.

This is often used when wanting to [manage expressions on the Server](https://www.adaptabletools.com/docs/adaptable-server-evaluation/index.md)

### GridSortedInfo

The [`GridSortedInfo`](https://www.adaptabletools.com/docs/reference/gridsortedinfo.md) object provided by the event returns a single object:

| Property | Type | Description |
| --- | --- | --- |
| [adaptableSortState](https://www.adaptabletools.com/docs/reference/gridsortedinfo.md#adaptablesortstate) | [`AdaptableSortState`](https://www.adaptabletools.com/docs/reference/adaptablesortstate.md) | Column sorts and Custom Sorts after this change. |
| [adaptableContext](https://www.adaptabletools.com/docs/reference/gridsortedinfo.md#adaptablecontext) | `any` | Custom application Context provided in `AdaptableOptions.adaptableContext` |

The [`AdaptableSortState`](https://www.adaptabletools.com/docs/reference/adaptablesortstate.md) object provides information on what is being sorted in AdapTable.

| Property | Type | Description |
| --- | --- | --- |
| [columnSorts](https://www.adaptabletools.com/docs/reference/adaptablesortstate.md#columnsorts) | [`ColumnSort`](https://www.adaptabletools.com/docs/reference/columnsort.md)`[]` | Columns currently being sorted (with direction) |
| [customSortComparers](https://www.adaptabletools.com/docs/reference/adaptablesortstate.md#customsortcomparers) | [`ColumnValuesComparer`](https://www.adaptabletools.com/docs/reference/columnvaluescomparer.md)`[]` | Custom Sort Comparers which are currently applied |
| [customSorts](https://www.adaptabletools.com/docs/reference/adaptablesortstate.md#customsorts) | [`CustomSort`](https://www.adaptabletools.com/docs/reference/customsort.md)`[]` | Custom Sorts which are currently applied |

Each `columnSorts` entry is a [`ColumnSort`](https://www.adaptabletools.com/docs/reference/columnsort.md):

| Property | Type | Description |
| --- | --- | --- |
| [ColumnId](https://www.adaptabletools.com/docs/reference/columnsort.md#columnid) | `string` | Id of Column being sorted |
| [SortOrder](https://www.adaptabletools.com/docs/reference/columnsort.md#sortorder) | `'Asc' \| 'Desc'` | How Column is sorted - either &#39;Asc&#39; or &#39;Desc&#39; |

It contains 2 sections:

- Column Sorts active in AG Grid
- Custom Sorts which have been provided in Initial State (i.e. not supplied via [Comparers](https://www.adaptabletools.com/docs/technical-reference-custom-sorting/index.md))
  

Custom Sorts are always sent even if none are currently active

### Event Subscription

Subscribing to the Event is done the same way as with all [Adaptable Events](https://www.adaptabletools.com/docs/technical-reference-adaptable-events/index.md):

```ts
api.eventApi.on('GridSorted', (eventInfo: GridSortedInfo) => {
    // do something with the info
});
```

There are also some sorting-related functions in [Grid API](https://www.adaptabletools.com/docs/technical-reference-managing-grid-data/index.md#grid-api)
