Custom SortComparer in DataGrid

Like many others when sorting I prefer to keep null values in the end of table. For example there are few payment or delivery choices for client. ShowOrder field sort this choices in the most preferred way. If ShowOrder value is null, this choice is not available for client and will not show him at all. It shoold be show to admin only, but in the end of table.
Of course I could make separate sort order column based on my show order. But I found SortComparer property of GridColumn and few forum discossions about it on this forum for another platforms.
Please, describe how to implement it in Blazor. I tried this, but it makes mistake. SortDirection is deleted now because I have not found library for ISortDirection,

<GridColumn Field="@nameof(Culture.ShowOrder)" HeaderText="@(StringLocalizer["Show Order"])" Type="ColumnType.Number" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Center" AllowFiltering="false"
                        SortComparer="@(new Application.Helpers.SortComparer<int?>())" >

 public class SortComparer<T> : IComparer<T>
    {
        public int Compare(T x, T y)
        {
            int result; 
            if (x == null && y == null) return 0;
            else if (x == null) return 1;
            else if (y == null) return -1;
            else if (double.TryParse(Convert.ToString(x, CultureInfo.InvariantCulture), NumberStyles.Any, NumberFormatInfo.InvariantInfo, out double xNum) &&
                    double.TryParse(Convert.ToString(y, CultureInfo.InvariantCulture), NumberStyles.Any, NumberFormatInfo.InvariantInfo, out double yNum))
            {
                result = xNum.CompareTo(yNum);
            }
            else if (typeof(T) == typeof(DateTime))
            {
                result = ((DateTime)(object)x).CompareTo((DateTime)(object)y);
            }
            else if (typeof(T) == typeof(bool))
            {
                result = ((bool)(object)x).CompareTo((bool)(object)y);
            }
            else // (propertyType == typeof(string))
            {
                result = string.Compare(x.ToString(), y.ToString(), StringComparison.InvariantCulture);
            }
            return result; //(ListSortDirection.Ascending ? 1 : -1) * 
        }
        //private ListSortDirection _SortDirection;
        //public ListSortDirection SortDirection
        //{
        //    get { return _SortDirection; }
        //    set { _SortDirection = value; }
        //}
    }

1 Reply

RN Rahul Narayanasamy Syncfusion Team May 6, 2020 02:56 PM UTC

Hi Stanislav, 

Greetings from Syncfusion. 

We have validated your query and currently we have some problem with SortComparer property. So we suggest you to achieve your requirement by using Custom Binding concept. Find the below documentation for your reference. 

Reference

Please get back to us if you need further assistance. 

Regards, 
Rahul 


Loader.
Up arrow icon