Skip to main content
All docs
V24.2

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

DxTreeList.CustomSort Event

Allows you to implement custom logic used to sort data in the TreeList.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v24.2.dll

NuGet Package: DevExpress.Blazor

#Declaration

C#
[Parameter]
public Action<TreeListCustomSortEventArgs> CustomSort { get; set; }

#Parameters

Type Description
TreeListCustomSortEventArgs

Provides access to TreeList data.

#Remarks

Follow the steps below to implement custom sorting logic:

  1. Set the SortMode property to Custom.
  2. Handle the CustomSort event to implement your logic that compares column values. When the event fires, the component compares two row values: the Value1 and Value2 properties. To define the column’s sort algorithm, set the Result property to one of the following values:

    Value Description
    -1 The TreeList places the first data item above the second item in ascending sort mode and below the second item in descending sort mode.
    1 The TreeList places the first data item below the second item in ascending sort mode and above the second item in descending sort mode.
    0 Indicates that the data items are equivalent according to the comparison condition. The TreeList sorts them based on their indices in the data source.
  3. Set the Handled property to true to indicate that the current comparison operation is handled. If the value is set to false, the component ignores the result of the custom comparison and uses the default mechanism to compare item values.

The following code snippet implements custom logic to sort the Start Date column’s values without taking into account the year part of the date:

@inject EmployeeTaskService EmployeeTaskService

<DxTreeList Data="TreeListData" KeyFieldName="Id" ParentKeyFieldName="ParentId" CustomSort="OnCustomSort">
    <Columns>
        <DxTreeListDataColumn FieldName="Name" Caption="Task" />
        <DxTreeListDataColumn FieldName="EmployeeName" />
        <DxTreeListDataColumn FieldName="StartDate"
                              DisplayFormat="d"
                              SortIndex="0"
                              SortMode="TreeListColumnSortMode.Custom"/>
        <DxTreeListDataColumn FieldName="DueDate" />
    </Columns>
</DxTreeList>

@code {
    List<EmployeeTask> TreeListData { get; set; }

    protected override void OnInitialized() {
        TreeListData = EmployeeTaskService.GenerateData();
    }

    void OnCustomSort(TreeListCustomSortEventArgs e) {
        if (e.FieldName == "StartDate") {
            var date1 = Convert.ToDateTime(e.Value1);
            var date2 = Convert.ToDateTime(e.Value2);
            if (date1.Month == date2.Month)
                e.Result = date1.Day.CompareTo(date2.Day);
            else
                e.Result = date1.Month.CompareTo(date2.Month);
            e.Handled = true;
        }
    }
}

For more information about data sorting in the TreeList component, refer to the following topic: Sort Data in Blazor TreeList.

See Also