Better control over custom sorting

I have two questions related to custom sorting.

First, I have a dataset feeding a grid where most columns will have numeric values, but some will be null. When I sort the columns, the nulls appear at the top, but I'd ideally like them to be ignored or sorted to the bottom. 

I tried using my own SortComparer with logic to always put the nulls at the end, however when I sort the sort direction is always ascending even when the grid is sorting descending. Is there another way to achieve the results I'm looking for?

Second, for some columns, an ascending sort isn't useful to look at and I'd ideally like to just allow sorting descending. Or, at least, I'd like the first tap of the column header to trigger a descending sort and then an ascending sort. Is there a way to control that?

Thank you for your help!



3 Replies

SR Sivakumar R Syncfusion Team July 9, 2018 07:28 AM UTC

Hi Ken, 
 
Can you please make sure you have implemented ISortDirection interface for your customer comparer and returning value based on SortDirection property. Please refer below documentation link for code snippet. 
 
public class CustomComparer : IComparer<Object>, ISortDirection 
{ 
    public int Compare(object x, object y) 
    { 
        int nameX; 
        int nameY; 
 
        //For OrderInfo type data 
        if (x.GetType () == typeof(OrderInfo)) { 
            //Calculating the length of FirstName in OrderInfo objects 
            nameX = ((OrderInfo)x).FirstName.Length; 
            nameY = ((OrderInfo)y).FirstName.Length; 
        } 
 
        //For Group type data                                    
        else if (x.GetType () == typeof(Group)) { 
            //Calculating the group key length 
            nameX = ((Group)x).Key.ToString ().Length; 
            nameY = ((Group)y).Key.ToString ().Length; 
        } else { 
            nameX = x.ToString ().Length; 
            nameY = y.ToString ().Length; 
        } 
 
        // Objects are compared and return the SortDirection 
        if (nameX.CompareTo (nameY) > 0) 
            return SortDirection == ListSortDirection.Ascending ? 1 : -1; 
        else if (nameX.CompareTo (nameY) == -1) 
            return SortDirection == ListSortDirection.Ascending ? -1 : 1; 
        else 
            return 0;  
    } 
 
    //Gets or sets the SortDirection value 
    public ListSortDirection SortDirection { get; set; } 
} 
 
 
Thanks, 
Sivakumar 



KP Ken Pespisa July 10, 2018 06:42 PM UTC

Yes, I am implementing the ISortDirection interface.

When I check SortDirection's value within the Compare method, it is always set to Ascending, even when the column has been changed to descending.


SR Sivakumar R Syncfusion Team July 11, 2018 12:47 PM UTC

Hi Ken, 
 
Sorry for the inconvenience. Yes, you are correct. ISortDirection will get updated only in grouping cases. Please find details below, 
 
DataGrid handles sorting for record using orderby linq query when grouping is not applied where it will handle ascending and descending changes in source itself. In grouping cases alone, ISortDirection will take effective and it will work considering SortDirection property.  
 
Please find the sample with grouping below where SortDirection updated on grouping cases. If you comment grouping in the sample, the SortDirection property is not updated and still sorting continue to work based on length of the string.  
 
We will update the same in our documentation and we already logged improvement tasks for handling sort direction handling in source itself for grouping cases also.  
 
Thanks, 
Sivakumar 


Loader.
Up arrow icon