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.
<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> |
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;
}
} |