# builtInSort

> _Since v0.2.7_

The default `sortHandler` used when none is provided in GridConfig.sortHandler.
Reads each column's `sortComparator` (falling back to defaultComparator)
and returns a sorted copy of the rows array.

**Contract:** This function is non-mutating — it copies the input array
(`[...rows].sort()`) before sorting and returns the new array. Public callers
may pass any array safely; internal callers that already own a mutable copy
should bypass this function and sort in place to avoid the extra allocation.
The non-mutation guarantee is part of the public API and must be preserved.

Use this as a fallback inside a custom `sortHandler` when you only need to
intercept sorting for specific columns or add pre/post-processing:

```ts
function builtInSort(rows: T[], sortState: SortState, columns: ColumnConfig<T>[]): T[]
```

## Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| `rows` | <code>T[]</code> |  |
| `sortState` | <code><a href="/grid/api/core/interfaces/sortstate/">SortState</a></code> |  |
| `columns` | <code><a href="/grid/api/core/interfaces/columnconfig/">ColumnConfig</a>&lt;T&gt;[]</code> |  |

## Example

```typescript
import { builtInSort } from '@toolbox-web/grid';
import type { SortHandler } from '@toolbox-web/grid';

const customSort: SortHandler<Employee> = (rows, state, columns) => {
  // Server-side sort for the "salary" column, client-side for everything else
  if (state.field === 'salary') {
    return fetch(`/api/employees?sort=${state.field}&dir=${state.direction}`)
      .then(res => res.json());
  }
  return builtInSort(rows, state, columns);
};

grid.gridConfig = { sortHandler: customSort };
```

## See Also

- [`GridConfig.sortHandler`](/grid/api/core/interfaces/gridconfig.md#sorthandler) for configuring the handler
- [`defaultComparator`](/grid/api/core/functions/defaultcomparator.md) for the comparator used per column
