Sorting string column

Hi,

I have got a class results data source which is bound to a GGC(editable). One of the column is Marks, which is string datatype.
I want to sort the marks based on a numeric sorting even though its a string column(as normal string sorting will result in wrong sort order). 
Any help is much appreciated.

Thanks & regards,
Meetu

The grid columns looks like as below

StudNo

StudName

ProgramCode

Marks

12345

AAAA

SRTGV

91.02

57657

BBBB

DFGG

2.00

575765

FFBDD

FTRRE

91.02

7688676

DDEEE

RGGG

0.00

434545

EEEEE

EDFG




1 Reply

MG Mohanraj Gunasekaran Syncfusion Team September 13, 2018 07:18 AM UTC

Hi Meetu, 
 
Thanks for using Syncfusion product. 
 
To sort the string type column based on the numeric value, you could implement the custom sorting for that column using Comparer property. Please refer the following code example and the sample, 
 
Code example 
SortColumnDescriptor scd = new SortColumnDescriptor("Mark"); 
scd.Comparer = new CustomSort(); 
scd.SortDirection = ListSortDirection.Ascending; 
this.gridGroupingControl1.TableDescriptor.SortedColumns.Add(scd); 
 
public class CustomSort : IComparer 
{ 
    public int Compare(object x, object y) 
    { 
        double value1; 
        double value2; 
        if (double.TryParse(x.ToString(), out value1) && double.TryParse(y.ToString(),out value2)) 
        { 
            if (value1 > value2) 
                return 1; 
            else if (value1 < value2) 
                return -1; 
        } 
        return 0; 
    } 
} 
 
 
Regards, 
Mohanraj G 


Loader.
Up arrow icon