How to: Implement Custom Sorting

This example shows how to implement custom sorting. The "Country" column displays text values. When sorting is applied to this column, the rows are compared by the length of the "Country" column values.

It is assumed that custom sorting is enabled for the "Country" column (its GridDataColumnSettings.SortMode property is set to 'Custom').

The image below shows the result:


protected void grid_CustomColumnSort
(object sender, DevExpress.Web.CustomColumnSortEventArgs e) {
    if (e.Column.FieldName == "Country") {
        e.Handled = true;
        string s1 = e.Value1.ToString(), s2 = e.Value2.ToString();
        if (s1.Length > s2.Length)
            e.Result = 1;
            if (s1.Length == s2.Length)
                e.Result = Comparer.Default.Compare(s1, s2);
                e.Result = -1;