Fixed secondary sort order

My grid columns are all sortable and my `DataSource` is a simple `IEnumerable<TItem>`.

I want to enforce a sort order where no matter which grid column the user sorts on, certain items appear at the top of the grid based on a boolean property.

In pseudo-code: `.OrderBy([grid column sort]).ThenBy("NeedsAttention")`

This is a pretty simple requirement, but as far as I can tell there is no simple way to achieve this. Implementing a `CustomComparer` that can handle every individual grid column is not practical for this.

I tried custom sorting the DataSource dynamically in `OnActionBegin` and `OnActionComplete` (per here, and here) but any custom sorting is overridden by the grid column sorting.

Is there a simple way to achieve this?


5 Replies

SP Sarveswaran Palani Syncfusion Team April 26, 2022 12:47 PM UTC

Hi Matt,


Greetings from Syncfusion support.


Currently we are validating your query at our end and we will update further details within two business days on or before (28.04.2022). Until then we appreciate your patience.


Regards,

Sarveswaran PK



MA Matt April 26, 2022 12:49 PM UTC

Thank you.


I realise I made a mistake in my initial post. The pseudo-code should read:


`.OrderBy("NeedsAttention").ThenBy([grid column sort])`


Kind regards

Matt




RS Renjith Singh Rajendran Syncfusion Team April 28, 2022 12:52 PM UTC

Hi Matt,


We suspect that you would like to prevent some rows in Grid from being included when performing column sorting action. We would like to inform you that, when perform sorting action in grid the sorting will be performed based on the entire column data only. This is default sorting behavior in grid.


Please get back to us if you need further assistance or if we have misunderstood your query.


Regards,

Renjith R



MA Matt April 28, 2022 01:08 PM UTC

Hi Renjith, thanks for your reply.


The objective is to always apply a fixed secondary sort when the user clicks on any column header. 

I do not want to exclude any rows from sorting.


To give an example, let's say my rows have a flag - "HasException" - to indicate that the record has a problem.


I always want these rows to be pinned to the top of the grid since the user must attend to them. But the grid columns must still be sortable. So I want this sort order:


    rows.OrderBy(row=>row.HasException).ThenBy(<<whatever column the user clicked on>>)


I have not been able to find a practical way to achieve this, as mentioned in my initial post.




RS Renjith Singh Rajendran Syncfusion Team April 29, 2022 07:10 AM UTC

Hi Matt,


Based on your scenario, we suggest you to use custom way of binding to achieve this requirement. You can customize the data related operations based on your requirement when bind data using CustomAdaptor. Every time performing data related operation will trigger the Read/ReadAsync method of CustomAdaptor. Now based on the DataManagerRequest you can customize the sort query and apply to query the DataSource. The DataSource which you return from the Read/ReadAsync method will only be displayed in grid.

References : https://blazor.syncfusion.com/documentation/datagrid/custom-binding


Sample : https://www.syncfusion.com/downloads/support/directtrac/general/ze/ServerApp-1374088345


Please refer the code below.


 

        public override object Read(DataManagerRequest dm, string key = null)

        {

            IEnumerable<Order> DataSource = Orders;

            ...

           if (dm.Sorted != null && dm.Sorted.Count > 0)

            {

                // Added a custom sort query in dm.Sorted

                dm.Sorted.Add(new Sort() { Comparer = null, Name = "CustomerID", Direction = SortDirection.Ascending.ToString() });

                DataSource = DataOperations.PerformSorting(DataSource, dm.Sorted);

            }

            ...

           return dm.RequiresCounts ? new DataResult() { Result = DataSource, Count = count } : (object)DataSource;

        }

 


Please get back to us if you need further assistance.


Regards,

Renjith R


Loader.
Up arrow icon