# Programmatic Sorting

Besides the built-in sorting functionality, you are able to programmatically sort the data in **RadGridView** using the **SortDescriptors** collection. This collection of **ISortDescriptor** objects allows you to use descriptors that define the sorting property (or column) and the sorting direction for the bound data. Because this is a collection, you are able not only to add, but you can also remove or clear the entries.

> tip If you use **ICollectionView** as data source, RadGridView will automatically synchronize the **SortDescriptors** of the source with its own ones.

> As of Q3 2010, RadGridView adds/removes **ColumnSortDescriptor** to its **SortDescriptors** collection when the user sorts from the UI.

> You can set the **SortMemberPath** property of the column to specify the name of the property the data in the column will be sorted by (applies to **ColumnSortDescriptor** only).

When you add a new descriptor to the collection, the data is automatically sorted according to it. To learn how to create and configure descriptors, look at the following example:

```C#
ColumnSortDescriptor csd = new ColumnSortDescriptor()
{
    Column = this.clubsGrid.Columns["Name"],
    SortDirection = ListSortDirection.Descending
};
this.clubsGrid.SortDescriptors.Add(csd);
```

```VB.NET

Dim csd As New ColumnSortDescriptor() With { _
 .Column = Me.clubsGrid.Columns("Name"), _
 .SortDirection = ListSortDirection.Descending _
}
Me.clubsGrid.SortDescriptors.Add(csd)
```

> caption Programmatically sorted RadGridView

![Telerik UI for WPF RadGridView programmatically sorted by the Name column in descending order](https://www.telerik.com/products/wpf/documentation/assets/aec0aabf71636248e07f5186b0d06dac/radgridview_programmaticsorting_1.png)

Another approach is to add the new **SortDescriptor** object (instead of **ColumnSortDescriptor**) to the RadGridView\.SortDescriptors collection:

```C#
SortDescriptor descriptor = new SortDescriptor();
descriptor.Member = "Title";
descriptor.SortDirection = ListSortDirection.Ascending;
```

```VB.NET
Dim descriptor As New SortDescriptor()
descriptor.Member = "Title"
descriptor.SortDirection = ListSortDirection.Ascending
```

The **Member** property defines the property by which the data will be sorted.
The **SortDirection** property allows you to define the sorting direction.

> When **SortMemberPath** is specified, you should apply **ColumnSortDescriptor** so that the information from the **SortMemberPath** is respected.

You can easily create a sort descriptor in XAML and then add it to the SortDescriptors collection.
For example:

```XAML
<telerik:RadGridView x:Name="radGridView"
             AutoGenerateColumns="False">

    <telerik:RadGridView.SortDescriptors>
        <telerik:SortDescriptor Member="Title"
                        SortDirection="Ascending" />
    </telerik:RadGridView.SortDescriptors>
</telerik:RadGridView>
```

As of **Q3 2011**, you can create a ColumnSortDescriptor in XAML and then add it to the SortDescriptors collection.

For example:

```XAML
<telerik:RadGridView x:Name="clubsGrid" 
                AutoGenerateColumns="False">
    <telerik:RadGridView.Columns>

    </telerik:RadGridView.Columns>
    <telerik:RadGridView.SortDescriptors>
        <telerik:ColumnSortDescriptor Column="{Binding Columns[\Title\], ElementName=clubsGrid}" SortDirection="Ascending"/>
    </telerik:RadGridView.SortDescriptors>
</telerik:RadGridView>
```

### XAML Tip

> tip In case of a static data structure, known during design time, it is better to declare your default sorting in XAML, rather than in your code-behind.

After the application runs with this descriptor defined, **RadGridView** data will be sorted ascending by the **Title** column and will look as if the user clicked on the **Title** column header.

> Adding or removing descriptors from the **SortDescriptors** collection won't raise **Sorting** and **Sorted** events, although the data will be sorted.

> The built-in sorting also uses the **SortDescriptors** collection. When a header is clicked, it clears the **SortDescriptors** collection and adds a new **ColumnSortDescriptor** to it.

> tip You are able to add **SortDescriptors/ColumnSortDescriptor** in XAML only at design time.
> tip When implementing multi-column sorting behavior you have to manage the **SortDescriptors** collection at runtime. To learn more, look at the [Multi-column Sorting](https://www.telerik.com/products/wpf/documentation/controls/radgridview/sorting/multiple-column-sorting) topic.

## Clearing the SortDescriptors Collection of the RadGridView

If you need to apply multiple sorting operations and you would like to reset all sorting rules applied, the **SortDescriptors** collection of the RadGridView should be cleared.

Consider the following scenario. You apply a programmatic sorting for your RadGridView as follows:

```C#
ColumnSortDescriptor csd = new ColumnSortDescriptor();
clubsGrid.SortDescriptors.Clear();
csd.Column = clubsGrid.Columns["Name"];
csd.SortDirection = ListSortDirection.Descending;
clubsGrid.SortDescriptors.Add(csd);
```

```VB.NET
Dim csd As New ColumnSortDescriptor()
clubsGrid.SortDescriptors.Clear()
csd.Column = clubsGrid.Columns("Name")
csd.SortDirection = ListSortDirection.Descending
clubsGrid.SortDescriptors.Add(csd)
```

At this point, the RadGridView has the following state:

> caption Telerik UI for  RadGridView programmatically sorted by the name column in descending order

![Telerik UI for WPF RadGridView programmatically sorted by the Name column in descending order](https://www.telerik.com/products/wpf/documentation/assets/aec0aabf71636248e07f5186b0d06dac/radgridview_programmaticsorting_1.png)

Eventually, you need to sort the grid by another column and would like to remove the previously applied rule. Use the following approach:

```C#
ColumnSortDescriptor csd = new ColumnSortDescriptor();
clubsGrid.SortDescriptors.Clear();
csd.Column = clubsGrid.Columns["Established"];
csd.SortDirection = ListSortDirection.Descending;
clubsGrid.SortDescriptors.Add(csd);
```

```VB.NET
Dim csd As New ColumnSortDescriptor()
clubsGrid.SortDescriptors.Clear()
csd.Column = clubsGrid.Columns("Established")
csd.SortDirection = ListSortDirection.Descending
clubsGrid.SortDescriptors.Add(csd)
```

The result will be:

> caption Telerik UI for  RadGridView programmatically resorted by the established column after clearing previous sort descriptors

![Telerik UI for WPF RadGridView programmatically resorted by the Established column after clearing previous sort descriptors](https://www.telerik.com/products/wpf/documentation/assets/d7f31398ba61218dc36dd0c378fa58d3/radgridview_programmaticsorting_2.png)

## See Also

* [Basic Sorting](https://www.telerik.com/products/wpf/documentation/controls/radgridview/sorting/basics)
* [Custom Sorting](https://www.telerik.com/products/wpf/documentation/controls/radgridview/sorting/custom)
* [Multiple-column Sorting](https://www.telerik.com/products/wpf/documentation/controls/radgridview/sorting/multiple-column-sorting)
