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.
Unfortunately, activation email could not send to your email. Please try again.

Sort Comparer for columns and groups

Thread ID:

Created:

Updated:

Platform:

Replies:

122128 Feb 18,2016 09:30 AM Apr 27,2018 08:54 AM WPF 3
loading
Tags: SfDataGrid
Deniss Nikiforov
Asked On February 18, 2016 09:30 AM

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.

Sowndaiyan Paulpandi [Syncfusion]
Replied On February 21, 2016 08:38 PM


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


Salva
Replied On April 26, 2018 11:28 AM


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.


Jai Ganesh S [Syncfusion]
Replied On April 27, 2018 08:54 AM

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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;