Skip to content

Commit

Permalink
rename to SortComparer; null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tesar-tech committed Nov 13, 2024
1 parent 51f6c4b commit f01dc58
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
SortMode="DataGridSortMode.Single">
<DataGridCommandColumn />
<DataGridColumn Field="@nameof(Employee.Id)" Caption="#" Sortable="false" />
<DataGridNumericColumn TItem="Employee" Field="@nameof(Employee.FirstName)" Caption="Full Name" Filterable="false" Comparer="new EmployeeNameComparer()">
<DataGridNumericColumn TItem="Employee" Field="@nameof(Employee.FirstName)" Caption="Full Name" Filterable="false" SortComparer="new EmployeeNameComparer()">
<DisplayTemplate>
@($"{context.FirstName} {context.LastName}")
</DisplayTemplate>
Expand Down Expand Up @@ -39,9 +39,9 @@
public override int Compare(Employee x, Employee y)
{
// Null checks
if (x == null && y == null) return 0;
if (x == null) return -1;
if (y == null) return 1;
if (x is null && y is null) return 0;
if (x is null) return -1;
if (y is null) return 1;

// Compare by length of FirstName
int firstNameLengthComparison = x.FirstName.Length.CompareTo(y.FirstName.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@


<DocsPageSection>
<DocsPageSectionHeader Title="Comparer">
<DocsPageSectionHeader Title="Sort Comparer">
<Paragraph>
The <code>Comparer</code> feature allows you to define a custom sorting logic for a column by specifying an <code>IComparer&lt;TItem&gt;</code> implementation.
The <code>SortComparer</code> feature allows you to define a custom sorting logic for a column by specifying an <code>IComparer&lt;TItem&gt;</code> implementation.
This is useful when you want to sort data based on complex or calculated criteria that go beyond the default alphabetical or numerical order.
</Paragraph>
<Paragraph>
Expand All @@ -78,7 +78,7 @@
<DocsPageSectionContent FullWidth Outlined>
<DataGridComparerExample />
</DocsPageSectionContent>
<DocsPageSectionSource Code="DataGridComparerExample" />
<DocsPageSectionSource Code="DataGridSortComparerExample" />
</DocsPageSection>


Expand Down
16 changes: 8 additions & 8 deletions Source/Extensions/Blazorise.DataGrid/DataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2301,15 +2301,15 @@ private void FilterData( IQueryable<TItem> query )
{
if ( sortByColumn.CurrentSortDirection == SortDirection.Ascending )
{
query = sortByColumn.Comparer == null
query = sortByColumn.SortComparer == null
? query.OrderBy( x => sortFunction( x ) )
: query.OrderBy( x => x, sortByColumn.Comparer );
: query.OrderBy( x => x, sortByColumn.SortComparer );
}
else
{
query = sortByColumn.Comparer == null
query = sortByColumn.SortComparer == null
? query.OrderByDescending( x => sortFunction( x ) )
: query.OrderByDescending( x => x, sortByColumn.Comparer );
: query.OrderByDescending( x => x, sortByColumn.SortComparer );
}

firstSort = false;
Expand All @@ -2318,15 +2318,15 @@ private void FilterData( IQueryable<TItem> query )
{
if ( sortByColumn.CurrentSortDirection == SortDirection.Ascending )
{
query = sortByColumn.Comparer == null
query = sortByColumn.SortComparer is null
? ( query as IOrderedQueryable<TItem> ).ThenBy( x => sortFunction( x ) )
: ( query as IOrderedQueryable<TItem> ).ThenBy( x => x, sortByColumn.Comparer );
: ( query as IOrderedQueryable<TItem> ).ThenBy( x => x, sortByColumn.SortComparer );
}
else
{
query = sortByColumn.Comparer == null
query = sortByColumn.SortComparer is null
? ( query as IOrderedQueryable<TItem> ).ThenByDescending( x => sortFunction( x ) )
: ( query as IOrderedQueryable<TItem> ).ThenByDescending( x => x, sortByColumn.Comparer );
: ( query as IOrderedQueryable<TItem> ).ThenByDescending( x => x, sortByColumn.SortComparer );
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Extensions/Blazorise.DataGrid/DataGridColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ public SortDirection CurrentSortDirection
/// <summary>
/// Gets or sets the custom comparer used for sorting the items in this column.
/// </summary>
[Parameter] public IComparer<TItem> Comparer { get; set; }
[Parameter] public IComparer<TItem> SortComparer { get; set; }

/// <summary>
/// Gets or sets whether the sort direction will be reversed.
Expand Down

0 comments on commit f01dc58

Please sign in to comment.