Sort Comparer for columns and groups
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.
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
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.
|
<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; }
}
} |
- 3 Replies
- 4 Participants
-
DN Deniss Nikiforov
- Feb 18, 2016 02:30 PM UTC
- Apr 27, 2018 12:54 PM UTC