// 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 OrderBySortDefinitions(this IEnumerable source, GridState state) => OrderBySortDefinitions(source, state?.SortDefinitions); public static IEnumerable OrderBySortDefinitions(this IEnumerable source, ICollection> sortDefinitions) { if (null == source || !source.Any()) return source; if (null == sortDefinitions || 0 == sortDefinitions.Count) return source; IOrderedEnumerable 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; } }