# .NET MAUI DataGrid Sorting

The [Telerik UI for .NET MAUI DataGrid](https://www.telerik.com/maui-ui/documentation/controls/datagrid/overview) provides a built-in sorting functionality which allows the user to order the view of the presented data and which is available to implement both through the UI and programmatically.

## Sorting through the UI

To sort through the UI, click or tap the column header. `UserSortMode` defines how the user input, such as tapping or clicking the column header, affects the current sort state of the DataGrid.

The `UserSortMode` has the following options:

* (Default) `Auto`—Sorting is applied according to the default platform behavior.
* `Multiple`—Allows sorting by multiple columns simultaneously.
* `None`—Disables sorting through the UI.
* `Single`—Allows sorting by only one column at a time.

You can also enable or disable the sorting of a specific column and define a value which indicates whether the user can sort the data by the column values by using the `CanUserSort` (bool) property.

## Programmatic Sorting

To sort the DataGrid programmatically, use either of the following approaches:

* Sort by a property with the [`PropertySortDescriptor`](https://www.telerik.com/maui-ui/documentation/controls/datagrid/sorting#property-sort-descriptor) added to the `RadDataGrid.SortDescriptors` collection.
* Sort by a custom key with the [`DelegateSortDescriptor`](https://www.telerik.com/maui-ui/documentation/controls/datagrid/sorting#delegate-sort-descriptor) added to the `RadDataGrid.SortDescriptors` collection.

### Sorting by Properties

You can sort the data in a DataGrid by pointing a property from the class that defines your objects. To achieve this scenario, use the `PropertySortDescriptor` and set its `PropertyName` property.

The descriptor exposes the following important properties:

* `PropertyName`—Gets or sets the name of the property that is used to retrieve the key to sort by.
* `SortOrder`—Gets or sets the order of the sorting (ascending or descending).

The following snippet shows how to set the `PropertySortDescriptor`.

```XAML
<telerik:RadDataGrid.SortDescriptors>
	<telerik:PropertySortDescriptor PropertyName="Name"/>
</telerik:RadDataGrid.SortDescriptors>
```

Then, define the `telerik` namespace:

```XAML
xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
```

### Sorting by Custom Keys

The difference between the `DelegateSortDescriptor` and the `PropertySortDescriptor` is that the `DelegateSortDescriptor` sorts the data by a custom key, while the `PropertySortDescriptor` sorts the data by a defined key, which is a property from the model.

The `DelegateSortDescriptor` exposes the following properties:

* `KeyLookup`—Gets or sets the `IKeyLookup` instance that is used to retrieve the sort key for each data item.
* `SortOrder`—Gets or sets the order of the sorting (ascending or descending).

To use a `DelegateSortDescriptor`, create a class that implements the `IKeyLookup` interface which will return the key by which you want to sort. Then, add the `DelegateSortDescriptor` to the `RadDataGrid.SortDescriptors` collection and set its `KeyLookup` property.

The following example demonstrates a custom `IKeyLookup` implementation.

```C#
public class CustomIKeyLookup : Telerik.Maui.Controls.Data.IKeyLookup
{
	public object GetKey(object instance)
	{
		return (instance as Club).Name.Length;
	}
}
```

Add it to the `SortDescriptors` collection of the DataGrid:

```C#
this.dataGrid.SortDescriptors.Add(new DelegateSortDescriptor() { KeyLookup = new CustomIKeyLookup()});
```

The following image shows how the DataGrid looks after sorting is applied:

![.NET MAUI DataGrid Sorting](https://www.telerik.com/maui-ui/documentation/assets/72e694764c83bd17c67bcdc8bba7f08c/datagrid_sorting.png)

## See Also

* [Setting the Telerik UI for .NET MAUI DataGrid Columns](https://www.telerik.com/maui-ui/documentation/controls/datagrid/columns/overview)
* [Grouping in the DataGrid](https://www.telerik.com/maui-ui/documentation/controls/datagrid/grouping/overview)
* [Aggregating Data in the DataGrid](https://www.telerik.com/maui-ui/documentation/controls/datagrid/aggregates/overview)
* [Filtering .NET MAUI DataGrid Records](https://www.telerik.com/maui-ui/documentation/controls/datagrid/filtering/overview)
