# Angular Grid Manual Sorting

The manual sorting gives the developer full control over the arranging of the data in the desired order. Compared to the [sorting with the built-in directive](https://www.telerik.com/kendo-angular-ui-develop/components/grid/sorting/basics#automatic-data-processing), the manual approach provides greater transparency of the process because it requires you to handle the [Angular Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid) events manually. The manual sorting is especially useful when you require additional customizations of the sorting logic.

To enable sorting:

1. Set the [`sortable`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#sortable) property.
2. Bind the [`sort`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#filter) option to a collection of [`SortDescriptor`](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/sortdescriptor) objects.
3. Handle either of the following events:
   * [`sortChange`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#sortchange) event—Use this event when you enable only the sorting data operation.
   * [`dataStateChange`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#datastatechange) event—Use this event when you enable more than one data operation, for example, sorting and filtering. For more details, check the section on how to [handle multiple data operations](https://www.telerik.com/kendo-angular-ui-develop/components/grid/data-operations#handling-multiple-data-operations).

The steps above are identical regardless if you [process the data locally](https://www.telerik.com/kendo-angular-ui-develop/components/grid/sorting/manual-sorting#sorting-local-data) or send a [request to the server](https://www.telerik.com/kendo-angular-ui-develop/components/grid/sorting/manual-sorting#sorting-on-the-server) to sort the data.

## Sorting Local Data

To sort local data, use the built-in [`orderBy()`](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/orderby) or [`process()`](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/process) functions.

> When other data operations like paging, filtering, or grouping, are also enabled, handle the `dataStateChange` event instead of `sortChange` and use the `process()` method. For more details, check the section on how to [handle multiple data operations](https://www.telerik.com/kendo-angular-ui-develop/components/grid/data-operations#handling-multiple-data-operations).

The following example demonstrates how to sort the data by handling the `sortChange` event and using the `orderBy()` function.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/sorting/manual-sorting/source.txt)

## Sorting on the Server

Handle the [`sortChange`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#sortchange), or [`dataStateChange`](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent#datastatechange) event to send a request containing information about the current Grid state to the server. For more details, check the section on how to [handle multiple data operations using remote data](https://www.telerik.com/kendo-angular-ui-develop/components/grid/data-operations#processing-data-on-the-server).

## Custom Sorting

To implement custom sorting logic, use the [`compare`](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/sortdescriptor#compare) function of the [`SortDescriptor`](https://www.telerik.com/kendo-angular-ui-develop/components/data-query/api/sortdescriptor). This callback defines how items are compared during sorting, which is useful for scenarios such as:

* Sorting string-based identifiers with numeric suffixes (`"ORD-1"`, `"ORD-10"`, `"ORD-2"` sorted as 1, 2, 10)
* Handling special values like `NaN` or `null`
* Applying domain-specific sorting rules

The logic within the `compare` function should be adjusted according to your specific sorting requirements.

The following example demonstrates how to sort order IDs with string prefixes numerically. Without the custom `compare` callback, the default alphabetical sorting would order `"ORD-10"` before `"ORD-2"`. With the custom logic, the numeric portions are extracted and sorted correctly (1, 2, 3... 10, 11... 100).

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/grid/sorting/custom-compare/source.txt)

## Suggested Links

* [Automatic Sorting](https://www.telerik.com/kendo-angular-ui-develop/components/grid/sorting/basics)
* [Multi-Column Sorting](https://www.telerik.com/kendo-angular-ui-develop/components/grid/sorting/multi-sort)
* [API Reference of the GridComponent](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api/gridcomponent)
* [API Index of the Grid](https://www.telerik.com/kendo-angular-ui-develop/components/grid/api)
