Reputation: 622
This might seem like an obvious question, and probably there will be previous questions about this topic, please point out and close if necesary because I can't find them.
Is there any Linq extension method that returns an ordered sequence by the elements themselves? I know it can be achieved by:
IEnumerable<int> myIntegers = new int[]{4, 6, 34, 987, -13};
IEnumerable<int> myOrderedIntegers = myIntegers.OrderBy(i => i);
I don't like this, it seems too verbose for such a simple operation. I know I could also do:
List<int> myOtherOrderedIntegers = myIntegers.ToList();
myOtherOrderedIntegers.Sort();
But uugh... bye bye deferred evaluation, and even more verbose. I could write my extension methods like...
public static IEnumerable<T> Order<T>(this IEnumerable<T> elements) => elements.OrderBy(e => e);
public static IEnumerable<T> OrderDescending<T>(this IEnumerable<T> elements) => elements.OrderByDescending(e => e);
Is there already any Linq extension method that can achieve this simple operation? I find it extrange that it isn't implemented already.
Thanks in advance.
Upvotes: 1
Views: 43
Reputation: 77294
Is there any other, less verbose method than myIntegers.OrderBy(i => i)
? Like myIntegers.Order()
? No there is not. You can write them yourself if you want to, you already pasted the code. You will have to live with the 8 bytes of wasted space in your source.
Upvotes: 1