How to: Sort Nodes Using Custom Rules

The following code shows how to implement custom node sorting by handling the ASPxTreeList.CustomNodeSort event. The "DEPARTMENT" column displays text values. When sorting is applied to this column, the nodes are compared by the length of the "DEPARTMENT" column's values rather than by the text itself.

The image below shows the result:

CustomNodeSort

using System.Collections;

protected void ASPxTreeList1_CustomNodeSort(object sender,
DevExpress.Web.ASPxTreeList.TreeListCustomNodeSortEventArgs e) {
    if (e.Column.FieldName != "DEPARTMENT") return;
    e.Handled = true;
    string value1 = e.Node1["DEPARTMENT"].ToString();
    string value2 = e.Node2["DEPARTMENT"].ToString();
    if (value1.Length > value2.Length)
        e.Result = 1;
    else
        if (value1.Length == value2.Length)
            e.Result = Comparer.Default.Compare(value1, value2);
        else
            e.Result = -1;
}