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

SfDataGrid: how to "customize" the sorting on a column?

Hello,

I would like to know if there is a way to customize the sorting on a column?

I encounter a problem on a column that is binded to a object, which is built on strings and numbers: the format is like string - number1 - number2

So the ordering doesn't take into account the value of the numbers, as the the object is a string:
Column sorting

=> But is it possible to "customize" the sorting process to respect the object format?


Regards,



3 Replies

JG Jai Ganesh S Syncfusion Team December 1, 2016 12:33 PM UTC

Hi Pierre, 
We have prepared a sample for applying the custom sorting for a column and its working fine in our end and please use your logic to this comparer for sort the column. 
this.sfGrid.SortComparers.Add(new SortComparer() { Comparer = new CustomComparer(), PropertyName = "Name" }); 
 
public class CustomComparer : IComparer<object>, ISortDirection 
{ 
    private ListSortDirection _SortDirection; 
 
     
    public ListSortDirection SortDirection 
    { 
        get { return _SortDirection; } 
        set { _SortDirection = value; } 
    } 
 
    public int Compare(object x, object y) 
    { 
        int namX; 
 
        int namY; 
 
        //While data object passed to comparer 
        if (x.GetType() == typeof(UserInfo)) 
        { 
            namX = ((UserInfo)x).Name.Length; 
            namY = ((UserInfo)y).Name.Length; 
        } 
 
        //While sorting groups 
        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; 
        } 
 
        //returns the comparison result based in 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; 
    } 
} 
 
Regards, 
Jai Ganesh S 



PD Pierre-Christophe DUS December 2, 2016 10:59 AM UTC

Thank you I've solved my issue.


JG Jai Ganesh S Syncfusion Team December 5, 2016 01:21 AM UTC

Hi Pierre, 
 
Thank you for the update. 
 
Please let us know if you need further assistance in this. 
 
Regards, 
Jai Ganesh S 


Loader.
Up arrow icon