-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Enumerable.Order and OrderDescending #67194
Comments
Tagging subscribers to this area: @dotnet/area-system-linq Issue DetailsBackground and motivationLinq provides API Proposalnamespace System.Linq
{
public partial class Enumerable
{
public static IEnumerable<T> Ordered<T>(this IEnumerable<T> source) where T: IComparable<T>;
public static IEnumerable<T> OrderedDescending<T>(this IEnumerable<T> source) where T: IComparable<T>;
}
} API Usagevar data = new[] { 2, 1, 3 };
// Instead of
// var sorted = data.OrderBy(e => e);
var sorted = data.Ordered();
// Instead of
// var sortedDescending = data.OrderByDescending(e => e);
var sortedDescending = data.OrderedDescending(); Alternative DesignsNo response RisksNo response
|
Shouldn't it return an |
Could they be called |
If we add this, I think the name should just be Order rather than Ordered. |
Maybe something explicit to avoid confusion with the existing methods: Alternatively just |
I think it would make more sense to allow for any T too, just like the existing APIs:
|
I agree, with a note that "Ordered" implies that the method will return data in some form, but does not communicate that there will be a substantial cost disproportional to size of the input. "Order" is suggesting that there is work behind constructing the result. |
Adding up, I feel explicitly defining the critetia ( |
So it more or less comes down to instead of calling a LINQ method with an identity function ( There are also other LINQ methods that could use this API change. For example But also |
Updated proposal based on your feedback. |
That almost happened (settled on the name |
Funny how you posted this and got 8 likes, but someone who said the exact same thing before you got no reactions :)) |
Can’t speak for @stephentoub but it usually happens to me due to overlap (ie while I’m typing someone posts before me). |
We posted at the same time. |
I see. Please accept my apologies. |
This is what I added to my own libraries years ago and it works fabulously. If it isn't an overload, it won't be discovered, and the programmer won't automatically assume it does the same general thing when reading the code. I am that programmer, speaking from experience. Side note, I also created a SortBy(...) which operates in-place on lists with a single selector. |
In principle I agree. However, naming also matters. |
|
I find
|
From my perspective, such a helper (to avoid |
@stephentoub I agree that this helper is not really necessary (and anyone can write it on his own, if he uses it a lot). I also understand the reasoning for the naming proposal, just wanted to throw another option out there. I don't agree that it goes against the semanitcs of LINQ, if you compare it to |
I always thought the reason those are |
Calling it differently than the existing |
namespace System.Linq
{
public partial class Enumerable
{
public static IOrderedEnumerable<T> Order<T>(this IEnumerable<T> source);
public static IOrderedEnumerable<T> Order<T>(this IEnumerable<T> source, IComparer<T> comparer);
public static IOrderedEnumerable<T> OrderDescending<T>(this IEnumerable<T> source);
public static IOrderedEnumerable<T> OrderDescending<T>(this IEnumerable<T> source, IComparer<T> comparer);
}
public partial class Queryable
{
// Or whatever's correct.
[DynamicDependency("Order`1", typeof(Enumerable))]
public static IOrderedQueryable<T> Order<T>(this IQueryable<T> source);
[DynamicDependency("Order`1", typeof(Enumerable))]
public static IOrderedQueryable<T> Order<T>(this IQueryable<T> source, IComparer<T> comparer);
[DynamicDependency("OrderDescending`1", typeof(Enumerable))]
public static IOrderedQueryable<T> OrderDescending<T>(this IQueryable<T> source);
[DynamicDependency("OrderDescending`1", typeof(Enumerable))]
public static IOrderedQueryable<T> OrderDescending<T>(this IQueryable<T> source, IComparer<T> comparer);
}
} |
Based on the decision made today, would it make sense to revisit |
...and there's the slippery slope. 5 minutes... that took even less time than I expected ;-) |
Doesn't seem like a big change. |
Originally posted by @rhuijben in #70525 (comment) |
@deeprobin The problem is, if the method is not added to |
Background and motivation
Linq provides
OrderBy
andOrderByDescending
which implies that I'm sorting based on some part of theT
. Very often I find myself wanting to just sort by theT
itself.API Proposal
API Usage
The text was updated successfully, but these errors were encountered: