You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Components/Extensions/DataGridExtensions.cs

37 lines
1.3 KiB

2 years ago
// Copyright (c) MudBlazor 2021
// MudBlazor licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Connected.Components;
namespace Connected;
public static class DataGridExtensions
{
public static IEnumerable<T> OrderBySortDefinitions<T>(this IEnumerable<T> source, GridState<T> state)
=> OrderBySortDefinitions(source, state?.SortDefinitions);
public static IEnumerable<T> OrderBySortDefinitions<T>(this IEnumerable<T> source, ICollection<SortDefinition<T>> sortDefinitions)
{
if (null == source || !source.Any())
return source;
if (null == sortDefinitions || 0 == sortDefinitions.Count)
return source;
IOrderedEnumerable<T> orderedEnumerable = null;
foreach (var sortDefinition in sortDefinitions)
{
if (null == orderedEnumerable)
orderedEnumerable = sortDefinition.Descending ? source.OrderByDescending(sortDefinition.SortFunc)
: source.OrderBy(sortDefinition.SortFunc);
else
orderedEnumerable = sortDefinition.Descending ? orderedEnumerable.ThenByDescending(sortDefinition.SortFunc)
: orderedEnumerable.ThenBy(sortDefinition.SortFunc);
}
return orderedEnumerable ?? source;
}
}