Added playlist logic.
This commit is contained in:
34
Harmonia.Core/Extensions/EnumerableExtensions.cs
Normal file
34
Harmonia.Core/Extensions/EnumerableExtensions.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Harmonia.Core.Models;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Harmonia.Core.Extensions;
|
||||
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
public static IOrderedEnumerable<T> SortBy<T>(this IEnumerable<T> source, SortOption[] sortOptions)
|
||||
{
|
||||
IOrderedEnumerable<T> orderedQuery = source.OrderBy(x => 0);
|
||||
Type type = typeof(T);
|
||||
|
||||
foreach (SortOption sortOption in sortOptions)
|
||||
{
|
||||
PropertyInfo? property = type.GetProperty(sortOption.FieldName);
|
||||
|
||||
if (property == null)
|
||||
continue;
|
||||
|
||||
switch (sortOption.Direction)
|
||||
{
|
||||
case ListSortDirection.Ascending:
|
||||
orderedQuery = orderedQuery.ThenBy(x => property.GetValue(x));
|
||||
break;
|
||||
case ListSortDirection.Descending:
|
||||
orderedQuery = orderedQuery.ThenByDescending(x => property.GetValue(x));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return orderedQuery;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user