New to Telerik UI for WinUI? Download free 30-day trial

DelegateSortDescriptor

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

Adding a DelegateSortDescriptor

To use a DelegateSortDescriptor, create a class that implements the IKeyLookup interface which will return the key you want to sort by. Then, you need to add the DelegateSortDescriptor to the SortDescriptors collection and set its KeyLookUp property as demonstrated in the following example. The DataGrid is populated with data in the same manner as demonstrated in the Populate the DataGrid with Data example from the PropertySortDescriptor article.

Define the DataGrid in XAML

<Grid xmlns:grid="using:Telerik.UI.Xaml.Controls.Grid" 
      xmlns:dataCore="using:Telerik.Data.Core"> 
    <grid:RadDataGrid Width="600" Height="460" x:Name="grid" > 
        <grid:RadDataGrid.SortDescriptors> 
            <dataCore:DelegateSortDescriptor SortOrder="Ascending"> 
                <dataCore:DelegateSortDescriptor.KeyLookup> 
                    <local:CustomIKeyLookup/> 
                </dataCore:DelegateSortDescriptor.KeyLookup> 
            </dataCore:DelegateSortDescriptor> 
        </grid:RadDataGrid.SortDescriptors> 
    </grid:RadDataGrid> 
</Grid> 
The following example demonstrates the implementation of the IKeyLookup interface. The GetKey method will receive an instance of the custom object. In this case, the key that is returned is the number of letters in each city.

Define the CustomIKeyLookup Class

public class CustomIKeyLookup : IKeyLookup 
{ 
    public object GetKey(object instance) 
    { 
        return (instance as CustomData).City.Length; 
    } 
} 
Sorted DataGrid

Delegate Sort Descriptor

See Also

In this article
Not finding the help you need?