Currently, if we get the direction of ordering as an external dependency, we should use , if we apply this direction:
public static IEnumerable<FileInfo> getlist(string directory, string searchPattern, string order) { var files = new DirectoryInfo(directory).EnumerateFiles(searchPattern); if (order == "A") return files.OrderBy(f => f.CreationTime); return files.OrderByDescending(f => f.CreationTime); }
Why is there no overload of OrderBy that takes the direction of the order as a parameter? In Reflector, I see that it is more or less implemented internally, but is not exposed to some strange reason.
I would rather write something like this:
public static IEnumerable<FileInfo> getlist(string directory, string searchPattern, string order) { return new DirectoryInfo(directory) .EnumerateFiles(searchPattern) .OrderBy(f => f.CreationTime, order == "A" ? SortOrder.Ascending : SortOrder.Descending); }
Update:
I can write this myself, I just hoped that it was already within:
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, ListSortDirection order) { switch (order) { case ListSortDirection.Ascending: return source.OrderBy(keySelector); case ListSortDirection.Descending: return source.OrderByDescending(keySelector); } throw new ArgumentOutOfRangeException("order"); }
source share