We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Sort Comparer for columns and groups

I am trying to implement proper sorting on columns and groups (when grouped by said column) and have an issue with the way the comparer receives it's info.

In the example here: http://help.syncfusion.com/wpf/sfdatagrid/sorting#custom-sorting , I need to specify a property name for which the comparer should get triggered. This part works, but what is strange is that inside the comparer, I do not get only the x and y from the object bound to that column, but instead an instance of a class that's bound to the full row (e.g. a single item from the items source collection).

Is the expected behavior, or am I doing something wrong. This doesn't seem right considering the requirement to specify the property name?  I would expect the comparer to receive only the object bound to that specific column.
I need to have different sorting for several columns in the context of 1 grid, do I then need to create many comparers vs just 1 and handling different types inside of it?

Thanks in advance.

3 Replies

SP Sowndaiyan Paulpandi Syncfusion Team February 22, 2016 01:38 AM UTC


Hi Deniss,

Thank for contacting Syncfusion Support.

Query 1 [Sort Comparer object contains the instance of class]

In SortComparer we have passed the whole instance of the corresponding class which is the behavior of SfDataGrid SortComparer. Because we have provided the support for comparing the all properties of that particular instance.


Query 2 [ One SortComparer for many Columns ]

In SfDataGrid you can able to handle sorting for different Columns using common SortComparer. We have prepared the sample as per your requirement and you can download the same from the below location,

Sample  : http://www.syncfusion.com/downloads/support/forum/122128/ze/SortingDemo-1024702161



Please refer the below link to know more about the SortComparer in SfDataGrid,

Link : http://help.syncfusion.com/wpf/sfdatagrid/sorting#custom-sorting


Regards,

Sowndaiyan



SA Salva replied to Sowndaiyan Paulpandi April 26, 2018 03:28 PM UTC


Hi Deniss,

Thank for contacting Syncfusion Support.

Query 1 [Sort Comparer object contains the instance of class]

In SortComparer we have passed the whole instance of the corresponding class which is the behavior of SfDataGrid SortComparer. Because we have provided the support for comparing the all properties of that particular instance.


Query 2 [ One SortComparer for many Columns ]

In SfDataGrid you can able to handle sorting for different Columns using common SortComparer. We have prepared the sample as per your requirement and you can download the same from the below location,

Sample  : http://www.syncfusion.com/downloads/support/forum/122128/ze/SortingDemo-1024702161



Please refer the below link to know more about the SortComparer in SfDataGrid,

Link : http://help.syncfusion.com/wpf/sfdatagrid/sorting#custom-sorting


Regards,

Sowndaiyan


Hello, I´m Salva.


I need to do something similar, but in the sample, it always order by "OrderDate", because in Compare always is typeof(Orders) and 

if (x.GetType() == typeof(Orders))
{
      // Calculating the Date of OrderDate if the object type is Orders
      // sorting can be performed based upon the "day"
      DateTime date1 = ((Orders)x).OrderDate.Value;
      DateTime date2 = ((Orders)y).OrderDate.Value;
      namX = date1.Day;
      namY = date2.Day;
}

In .xaml there are two SortComparers

     
           
               
               
           
            
           
               
               
           
            
   
   

I don´t know in Compare if sort is by OrderDate or CustomerID.

I want to do something like:

if (x.GetType() == typeof(Orders))
{
     if (OrderDate)
     {
          // Order by data.
     }

     if (CustomerId)
     {
          // Order by CustomerId.
      }
}

public int Compare(object x, object y)
        {
            int namX;
            int namY;

            //For Customers Type data

            if (x.GetType() == typeof(Orders))
            {

                //Calculating the Date of OrderDate if the object type is Orders
                //sorting can be performed based upon the "day"
                DateTime date1 = ((Orders)x).OrderDate.Value;
                DateTime date2 = ((Orders)y).OrderDate.Value;
                namX = date1.Day;
                namY = date2.Day;
            }

            //For Group type Data                                   
            else if (x.GetType() == typeof(Group))
            {

                //Calculating the group key length
                namX = ((Group)x).Key.ToString().Length;
                namY = ((Group)y).Key.ToString().Length;
            }
            else
            {
                namX = x.ToString().Length;
                namY = y.ToString().Length;
            }

            // Objects are compared and return the SortDirection
            if (namX.CompareTo(namY) > 0)
                return SortDirection == ListSortDirection.Ascending ? 1 : -1;

            if (namX.CompareTo(namY) == -1)
                return SortDirection == ListSortDirection.Ascending ? -1 : 1;
            
            return 0;

        }

PD: Sorry, but I don´t know how to show code in correct format with colors.



JG Jai Ganesh S Syncfusion Team April 27, 2018 12:54 PM UTC

Hi Salva, 
 
You can achieve your requirement to apply the custom sorting for more than one column using the same SortComparer by using the below code, 
 
<syncfusion:SfDataGrid.SortComparers> 
        <Linq:SortComparer  PropertyName="OrderDate" > 
                    <Linq:SortComparer.Comparer> 
                        <local:CustomerInfo SortString="OrderDate"/> 
                    </Linq:SortComparer.Comparer> 
        </Linq:SortComparer> 
                <Linq:SortComparer PropertyName="CustomerID"> 
                    <Linq:SortComparer.Comparer> 
                        <local:CustomerInfo SortString="CustomerID"/> 
                    </Linq:SortComparer.Comparer> 
                </Linq:SortComparer> 
</syncfusion:SfDataGrid.SortComparers> 
 
public class CustomerInfo : IComparer<Object>, ISortDirection 
{ 
    private string _sortstring; 
    public string SortString 
    { 
        get 
        { 
            return _sortstring; 
        } 
        set 
        { 
            _sortstring = value; 
        } 
    } 
    public int Compare(object x, object y) 
    { 
        int namX; 
        int namY; 
 
        //For Customers Type data 
 
        if (x.GetType() == typeof(Orders)) 
        { 
            if (SortString == "OrderDate") 
            { 
                //Calculating the Date of OrderDate if the object type is Orders 
                //sorting can be performed based upon the "day" 
                DateTime date1 = ((Orders)x).OrderDate.Value; 
                DateTime date2 = ((Orders)y).OrderDate.Value; 
                namX = date1.Day; 
                namY = date2.Day; 
            } 
            else 
            { 
                namX = ((Orders)x).CustomerID.ToString().Length; 
                namY = ((Orders)y).CustomerID.ToString().Length; 
            } 
        } 
 
        //For Group type Data                                    
        else if (x.GetType() == typeof(Group)) 
        { 
            //Calculating the group key length 
            namX = ((Group)x).Key.ToString().Length; 
            namY = ((Group)y).Key.ToString().Length; 
        } 
        else 
        { 
            namX = x.ToString().Length; 
            namY = y.ToString().Length; 
        } 
 
        // Objects are compared and return the SortDirection 
        if (namX.CompareTo(namY) > 0) 
            return SortDirection == ListSortDirection.Ascending ? 1 : -1; 
        else if (namX.CompareTo(namY) == -1) 
            return SortDirection == ListSortDirection.Ascending ? -1 : 1; 
        else 
            return 0; 
    } 
 
    //Get or Set the SortDirection value 
    private ListSortDirection _SortDirection; 
    public ListSortDirection SortDirection 
    { 
        get { return _SortDirection; } 
        set { _SortDirection = value; } 
    } 
 
} 
 
 
Regards, 
Jai Ganesh S 


Loader.
Live Chat Icon For mobile
Up arrow icon