It seems that this is what you need:
The method itself will look something like this:
Sort array will need to be changed beforehand, the first element must be OrderByDescending ( OrderByDescending ), the rest are ThenBy ( ThenByDescending )
public static IEnumerable<T> SortIt<T>(IEnumerable<T> items, Sort[] sorts) { var queryItems = items.AsQueryable(); foreach (var s in sorts) queryItems = queryItems.ApplyOrder(s.Property, s.Direction); return queryItems.AsEnumerable(); }
Still need extension:
public static class Extensions { public static IOrderedQueryable<T> ApplyOrder<T>( this IQueryable<T> source, string property, string methodName ) { var arg = Expression.Parameter(typeof(T), "x"); Expression expr = arg; expr = Expression.Property(expr, property); var lambda = Expression.Lambda(expr, arg); var method = typeof(Queryable).GetGenericMethod( methodName, new[] { typeof(T), expr.Type }, new[] { source.GetType(), lambda.GetType() } ); return (IOrderedQueryable<T>)method.Invoke(null, new object[] { source, lambda }); } public static MethodInfo GetGenericMethod( this Type type, string name, Type[] genericTypeArgs, Type[] paramTypes ) { var methods = from abstractGenericMethod in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) where abstractGenericMethod.Name == name where abstractGenericMethod.IsGenericMethod let pa = abstractGenericMethod.GetParameters() where pa.Length == paramTypes.Length select abstractGenericMethod.MakeGenericMethod(genericTypeArgs) into concreteGenericMethod where concreteGenericMethod.GetParameters() .Select(p => p.ParameterType).SequenceEqual(paramTypes, new TestAssignable()) select concreteGenericMethod; return methods.FirstOrDefault(); } private class TestAssignable : IEqualityComparer<Type> { public bool Equals(Type x, Type y) { return x.IsAssignableFrom(y); } public int GetHashCode(Type obj) { return obj.GetHashCode(); } } }
https://habrahabr.ru/post/181065/
Sort<T>that is now - GrundySort<T>method sorted only by the first property from theSort[]array, so it somehow doesn’t really fit into this question - Artur Shaikhutdinov