Sorting of column with null value

Hi,

I have a column in a grid where the datatype is a nullable DateTime.
Is it possible to always show the rows with null value on the bottom of the grid after sorting?

Like this:

MyDate (Ascending)
--------------
01.06.2022
02.06.2022
03.06.2022
<null>
<null>

MyDate (Descending)
--------------
03.06.2022
02.06.2022
01.06.2022
<null>
<null>

I have tried to use the SoftComparer with no luck. I think I need access to the SortDirection in the CustomComparer to get this to work with the SoftComparer.

Do you have any tricks for this.

Thanks.


1 Reply 1 reply marked as answer

MS Monisha Saravanan Syncfusion Team May 26, 2022 03:12 PM UTC

Hi Joheno,


Greetings from Syncfusion support.


We have checked your query and we suspect that you need to  display the null values at the bottom of the grid. Here we have prepared an sample to display the null values at bottom for both ascending and descending sort direction. Along with this we suggest you to handle your own sorting operation as per your requirement. Kindly check the attached code snippet and sample for your reference


 

<div class="row">

    <SfGrid DataSource="@Orders" @ref="Grid" AllowMultiSorting="true" AllowSorting="true" AllowGrouping="true"  Height="100%">

        <GridEvents OnActionBegin="ActionBegin" TValue="Order"></GridEvents>

 

        <GridColumns>

            <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2"  Width="80"></GridColumn>

            <GridColumn Field=@nameof(Order.OrderDate)  SortComparer="new CustomComparer()" HeaderText="OrderDate" Format="d"  Width="80"></GridColumn>

             </GridColumns>

    </SfGrid>

</div>

 

 

@code{

    public SfGrid<Order> Grid;

    public List<Order> Orders { get; set; }

    public static SortDirection SortDir { get; set; }

 

    public void ActionBegin(ActionEventArgs<Order> args)

    {

        if(args.RequestType == Syncfusion.Blazor.Grids.Action.Sorting)

        {

            SortDir = args.Direction;

 

        }

    }

    public class CustomComparer : IComparer<Object>

    {

             public int Compare(object XRowDataToCompare, object YRowDataToCompare)

        {

            var sortAsc = SortDir == SortDirection.Descending ? true : false;

            Order XRowData = XRowDataToCompare as Order;

            Order YRowData = YRowDataToCompare as Order;

            var reference = XRowData?.OrderDate;

            var comparer = YRowData?.OrderDate;

 

            if (sortAsc && reference == null) {

                return -1;

            } else if (sortAsc && comparer == null) {

                return 1;

            } else if (!sortAsc && reference == null) {

                return 1;

            } else if (!sortAsc && comparer == null) {

                return -1;

            } else {

                return (reference - comparer).Value.Milliseconds;

            }

        }

    }

 

 

    }


Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/DateRangePicker723414699.zip


Kindly get back to us if you have further queries.


Regards,

Monisha


Marked as answer
Loader.
Up arrow icon