How to group a column that contains English month names

I have an SfDataGrid that contains a column that contains month names in English (e.g., January, February, March, etc.) When I try to group by the month column, the grid gets sorted alphabetically by month name so all of the rows with April in the column come first, then all of the rows with December in the column come next, then all of the rows with February in the column come next, etc.  I want all of the January rows grouped first, then all the February rows, then all the March rows, etc.


1 Reply

LN Lakshmi Natarajan Syncfusion Team October 28, 2021 10:43 AM UTC

Hi Steven, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “How to group a column that contains English month names” from our side. We would like to inform you that you can achieve your requirement by adding SfDataGrid.SortComparers using custom comparer to sort the month. 
 
Please refer to our user guidance document regarding SortComparer in the following link, 
 
Please refer to the following code snippets to achieve your requirement, 
 
XAML: Add the CustomComparer to the SortComaparers. 
<ContentPage.Resources> 
    <ResourceDictionary> 
        <local:CustomComparer x:Key="Comparer" /> 
    </ResourceDictionary> 
</ContentPage.Resources> 
 
<StackLayout Orientation="Vertical"> 
    <syncfusion:SfDataGrid x:Name="dataGrid" ColumnSizer="Star" 
            EnableDataVirtualization="False" 
            AutoGenerateColumns ="False" 
            HeaderRowHeight="52" 
            SelectionMode="Single" 
            AllowEditing="True" 
            NavigationMode="Cell" 
            ItemsSource="{Binding OrdersInfo}" 
            EditTapAction="OnTap"> 
 
        <syncfusion:SfDataGrid.SortComparers> 
            <data:SortComparer Comparer="{StaticResource Comparer}" 
                            PropertyName="Month" /> 
        </syncfusion:SfDataGrid.SortComparers> 
 
        <syncfusion:SfDataGrid.GroupColumnDescriptions> 
            <syncfusion:GroupColumnDescription ColumnName="Month" /> 
        </syncfusion:SfDataGrid.GroupColumnDescriptions> 
... 
    </syncfusion:SfDataGrid> 
</StackLayout> 
 
CustomComaparer: You can parse the month using the DateTime.ParseExact and get the month order number from the Sort.Month property. 
public class CustomComparer : IComparer<Object> 
{ 
    public int Compare(object x, object y) 
    { 
        var groupXName = ((Group)x).Key.ToString(); 
        var groupYName = ((Group)y).Key.ToString(); 
 
        var xMonth = new { Name = groupXName, Sort = DateTime.ParseExact(groupXName.ToString(), "MMMM", CultureInfo.InvariantCulture) }; 
        var xMonthOrder = xMonth.Sort.Month; 
             
        var yMonth = new { Name = groupYName, Sort = DateTime.ParseExact(groupYName.ToString(), "MMMM", CultureInfo.InvariantCulture) }; 
        var yMonthOrder = yMonth.Sort.Month; 
 
        if (xMonthOrder.CompareTo(yMonthOrder) > 0) return 1; 
        else if (xMonthOrder.CompareTo(yMonthOrder) == -1) return -1; 
        else return 0; 
    } 
} 
 
You can also refer to the documentation to parse the month, 
 
We have prepared a sample and attached in the following link, 
 
Output 
 
 
You can also refer to the following documentation to group a column without sorting, 
 
Please let us know if you need further assistance. 
 
Regards, 
Lakshmi Natarajan 


Loader.
Up arrow icon