Why is there OrderBy and OrderByDescending, but not OrderBy (SortOrder)?

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"); } 
+4
source share
4 answers

Since the SortOrder enumeration can technically take more than two values ​​(think of (SortOrder) 35 ), this will not accurately reflect the duality. Having 2 methods, there is no ambiguity or the need to check the range (which is not the case in your example, by the way).

So here is the method you want:

 public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, SortOrder order) { if(order < SortOrder.Ascending || order > SortOrder.Descending) { throw new ArgumentOutOfRangeException("order"); } return order == SortOrder.Ascending ? source.OrderBy(keySelector) : source.OrderByDescending(keySelector); } 
+7
source

The OrderBy method already has the flexibility you require, and much more, since it can take the optional IComparer<T> argument:

 return new DirectoryInfo(directory) .EnumerateFiles(searchPattern) .OrderBy(f => f.CreationTime, order == "A" ? Comparer<DateTime>.Default : new DescendingComparer<DateTime>); // ... public DescendingComparer<T> : Comparer<T> { public override int Compare(T x, T y) { return Comparer<T>.Default.Compare(y, x); } } 
+5
source

I do not know why. Although, at that time, you can do it yourself.

 public static class IEnumerableSortExtension { public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, SortOrder order) { if (order == SortOrder.Ascending) return this.OrderBy(keySelector); else if (order == SortOrder.Descending) return this.OrderByDescending(keySelector); throw new InvalidOperationException(); // do something better than this } } 
+3
source

The other answers are more academic than mine, but if you need to quickly and dirty, you can do this:

 var files = new DirectoryInfo(directory) .EnumerateFiles(searchPattern) .OrderByDescending(f => f.CreationTime); if (order == "A") files = files.Reverse(); return files; 
+2
source

Source: https://habr.com/ru/post/1315724/


All Articles