Currently if we get direction of ordering as an external dependency we have to use if to 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 order direction as a parameter? In Reflector I see that it's more or less implemented internally but not exposed for some weird reason.
I would much rather prefer writing 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, just was hoping that it's already in the framework:
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");
}