// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace System.Linq
{
public static partial class AsyncEnumerable
{
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.orderby?view=net-9.0-pp#system-linq-asyncenumerable-orderby-2(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
// The method linked to above includes a comparer parameter with a default value of null.
///
/// Sorts the elements of a sequence in ascending order according to a key.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An async-enumerable sequence of values to order.
/// A function to extract a key from an element.
/// An ordered async-enumerable sequence whose elements are sorted according to a key.
/// or is null.
public static IOrderedAsyncEnumerable OrderBy(this IAsyncEnumerable source, Func keySelector) =>
new OrderedAsyncEnumerable(source, keySelector, comparer: null, descending: false, parent: null);
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
///
/// Sorts the elements of a sequence in ascending order according to a key obtained by invoking a transform function on each element and awaiting the result.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An async-enumerable sequence of values to order.
/// An asynchronous function to extract a key from an element.
/// An ordered async-enumerable sequence whose elements are sorted according to a key.
/// or is null.
[GenerateAsyncOverload]
[Obsolete("Use OrderBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByAwait functionality now exists as overloads of OrderBy.")]
private static IOrderedAsyncEnumerable OrderByAwaitCore(this IAsyncEnumerable source, Func> keySelector) =>
new OrderedAsyncEnumerableWithTask(source, keySelector, comparer: null, descending: false, parent: null);
#if !NO_DEEP_CANCELLATION
[GenerateAsyncOverload]
[Obsolete("Use OrderBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByAwaitWithCancellation functionality now exists as overloads of OrderBy.")]
private static IOrderedAsyncEnumerable OrderByAwaitWithCancellationCore(this IAsyncEnumerable source, Func> keySelector) =>
new OrderedAsyncEnumerableWithTaskAndCancellation(source, keySelector, comparer: null, descending: false, parent: null);
#endif
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.orderby?view=net-9.0-pp#system-linq-asyncenumerable-orderby-2(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
///
/// Sorts the elements of a sequence in ascending order by using a specified comparer.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An async-enumerable sequence of values to order.
/// A function to extract a key from an element.
/// A comparer to compare keys.
/// An ordered async-enumerable sequence whose elements are sorted according to a key.
/// or is null.
public static IOrderedAsyncEnumerable OrderBy(this IAsyncEnumerable source, Func keySelector, IComparer comparer) =>
new OrderedAsyncEnumerable(source, keySelector, comparer, descending: false, parent: null);
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
///
/// Sorts the elements of a sequence in ascending order by using a specified comparer. The keys are obtained by invoking the transform function on each element and awaiting the result.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An async-enumerable sequence of values to order.
/// An asynchronous function to extract a key from an element.
/// A comparer to compare keys.
/// An ordered async-enumerable sequence whose elements are sorted according to a key.
/// or is null.
[GenerateAsyncOverload]
[Obsolete("Use OrderBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByAwait functionality now exists as overloads of OrderBy.")]
private static IOrderedAsyncEnumerable OrderByAwaitCore(this IAsyncEnumerable source, Func> keySelector, IComparer comparer) =>
new OrderedAsyncEnumerableWithTask(source, keySelector, comparer, descending: false, parent: null);
#if !NO_DEEP_CANCELLATION
[GenerateAsyncOverload]
[Obsolete("Use OrderBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByAwaitWithCancellation functionality now exists as overloads of OrderBy.")]
private static IOrderedAsyncEnumerable OrderByAwaitWithCancellationCore(this IAsyncEnumerable source, Func> keySelector, IComparer comparer) =>
new OrderedAsyncEnumerableWithTaskAndCancellation(source, keySelector, comparer, descending: false, parent: null);
#endif
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.orderbydescending?view=net-9.0-pp#system-linq-asyncenumerable-orderbydescending-2(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
// The method linked to above includes a comparer parameter with a default value of null.
///
/// Sorts the elements of a sequence in descending order according to a key.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An async-enumerable sequence of values to order.
/// A function to extract a key from an element.
/// An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.
/// or is null.
public static IOrderedAsyncEnumerable OrderByDescending(this IAsyncEnumerable source, Func keySelector) =>
new OrderedAsyncEnumerable(source, keySelector, comparer: null, descending: true, parent: null);
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
///
/// Sorts the elements of a sequence in descending order according to a key obtained by invoking a transform function on each element and awaiting the result.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An async-enumerable sequence of values to order.
/// An asynchronous function to extract a key from an element.
/// An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.
/// or is null.
[GenerateAsyncOverload]
[Obsolete("Use OrderByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByDescendingAwait functionality now exists as overloads of OrderByDescending.")]
private static IOrderedAsyncEnumerable OrderByDescendingAwaitCore(this IAsyncEnumerable source, Func> keySelector) =>
new OrderedAsyncEnumerableWithTask(source, keySelector, comparer: null, descending: true, parent: null);
#if !NO_DEEP_CANCELLATION
[GenerateAsyncOverload]
[Obsolete("Use OrderByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByDescendingAwaitWithCancellation functionality now exists as overloads of OrderByDescending.")]
private static IOrderedAsyncEnumerable OrderByDescendingAwaitWithCancellationCore(this IAsyncEnumerable source, Func> keySelector) =>
new OrderedAsyncEnumerableWithTaskAndCancellation(source, keySelector, comparer: null, descending: true, parent: null);
#endif
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.orderbydescending?view=net-9.0-pp#system-linq-asyncenumerable-orderbydescending-2(system-collections-generic-iasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
///
/// Sorts the elements of a sequence in descending order by using a specified comparer.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An async-enumerable sequence of values to order.
/// A function to extract a key from an element.
/// A comparer to compare keys.
/// An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.
/// or is null.
public static IOrderedAsyncEnumerable OrderByDescending(this IAsyncEnumerable source, Func keySelector, IComparer comparer) =>
new OrderedAsyncEnumerable(source, keySelector, comparer, descending: true, parent: null);
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
///
/// Sorts the elements of a sequence in descending order by using a specified comparer. The keys are obtained by invoking the transform function on each element and awaiting the result.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An async-enumerable sequence of values to order.
/// An asynchronous function to extract a key from an element.
/// A comparer to compare keys.
/// An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.
/// or is null.
[GenerateAsyncOverload]
[Obsolete("Use OrderByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByDescendingAwait functionality now exists as overloads of OrderByDescending.")]
private static IOrderedAsyncEnumerable OrderByDescendingAwaitCore(this IAsyncEnumerable source, Func> keySelector, IComparer comparer) =>
new OrderedAsyncEnumerableWithTask(source, keySelector, comparer, descending: true, parent: null);
#if !NO_DEEP_CANCELLATION
[GenerateAsyncOverload]
[Obsolete("Use OrderByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the OrderByDescendingAwaitWithCancellation functionality now exists as overloads of OrderByDescending.")]
private static IOrderedAsyncEnumerable OrderByDescendingAwaitWithCancellationCore(this IAsyncEnumerable source, Func> keySelector, IComparer comparer) =>
new OrderedAsyncEnumerableWithTaskAndCancellation(source, keySelector, comparer, descending: true, parent: null);
#endif
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.thenby?view=net-9.0-pp#system-linq-asyncenumerable-thenby-2(system-linq-iorderedasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
// The method linked to above includes a comparer parameter with a default value of null.
///
/// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An ordered async-enumerable sequence that contains elements to sort.
/// A function to extract a key from each element.
/// An ordered async-enumerable whose elements are sorted according to a key.
/// or is null.
public static IOrderedAsyncEnumerable ThenBy(this IOrderedAsyncEnumerable source, Func keySelector)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: false);
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
///
/// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key obtained by invoking a transform function on each element and awaiting the result.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An ordered async-enumerable sequence that contains elements to sort.
/// An asynchronous function to extract a key from each element.
/// An ordered async-enumerable whose elements are sorted according to a key.
/// or is null.
[GenerateAsyncOverload]
[Obsolete("Use ThenBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByAwait functionality now exists as overloads of ThenBy.")]
private static IOrderedAsyncEnumerable ThenByAwaitCore(this IOrderedAsyncEnumerable source, Func> keySelector)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer: default(IComparer), descending: false);
}
#if !NO_DEEP_CANCELLATION
[GenerateAsyncOverload]
[Obsolete("Use ThenBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByAwaitWithCancellation functionality now exists as overloads of ThenBy.")]
private static IOrderedAsyncEnumerable ThenByAwaitWithCancellationCore(this IOrderedAsyncEnumerable source, Func> keySelector)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: false);
}
#endif
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.thenby?view=net-9.0-pp#system-linq-asyncenumerable-thenby-2(system-linq-iorderedasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
///
/// Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An ordered async-enumerable sequence that contains elements to sort.
/// A function to extract a key from each element.
/// A comparer to compare keys.
/// An ordered async-enumerable whose elements are sorted according to a key.
/// or is null.
public static IOrderedAsyncEnumerable ThenBy(this IOrderedAsyncEnumerable source, Func keySelector, IComparer comparer)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
///
/// Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer. The keys are obtained by invoking a transform function on each element and awaiting the result.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An ordered async-enumerable sequence that contains elements to sort.
/// An asynchronous function to extract a key from each element.
/// A comparer to compare keys.
/// An ordered async-enumerable whose elements are sorted according to a key.
/// or is null.
[GenerateAsyncOverload]
[Obsolete("Use ThenBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByAwait functionality now exists as overloads of ThenBy.")]
private static IOrderedAsyncEnumerable ThenByAwaitCore(this IOrderedAsyncEnumerable source, Func> keySelector, IComparer comparer)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
}
#if !NO_DEEP_CANCELLATION
[GenerateAsyncOverload]
[Obsolete("Use ThenBy. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByAwaitWithCancellation functionality now exists as overloads of ThenBy.")]
private static IOrderedAsyncEnumerable ThenByAwaitWithCancellationCore(this IOrderedAsyncEnumerable source, Func> keySelector, IComparer comparer)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer, descending: false);
}
#endif
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.thenbydescending?view=net-9.0-pp#system-linq-asyncenumerable-thenbydescending-2(system-linq-iorderedasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
// The method linked to above includes a comparer parameter with a default value of null.
///
/// Performs a subsequent ordering of the elements in a sequence in descending order, according to a key.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An ordered async-enumerable sequence that contains elements to sort.
/// A function to extract a key from each element.
/// An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.
/// or is null.
public static IOrderedAsyncEnumerable ThenByDescending(this IOrderedAsyncEnumerable source, Func keySelector)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: true);
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
///
/// Performs a subsequent ordering of the elements in a sequence in descending order, according to a key obtained by invoking a transform function on each element and awaiting the result.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An ordered async-enumerable sequence that contains elements to sort.
/// An asynchronous function to extract a key from each element.
/// An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.
/// or is null.
[GenerateAsyncOverload]
[Obsolete("Use ThenByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByDescendingAwait functionality now exists as overloads of ThenByDescending.")]
private static IOrderedAsyncEnumerable ThenByDescendingAwaitCore(this IOrderedAsyncEnumerable source, Func> keySelector)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer: default(IComparer), descending: true);
}
#if !NO_DEEP_CANCELLATION
[GenerateAsyncOverload]
[Obsolete("Use ThenByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByDescendingAwaitWithCancellation functionality now exists as overloads of ThenByDescending.")]
private static IOrderedAsyncEnumerable ThenByDescendingAwaitWithCancellationCore(this IOrderedAsyncEnumerable source, Func> keySelector)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer: null, descending: true);
}
#endif
#if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
// https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.thenbydescending?view=net-9.0-pp#system-linq-asyncenumerable-thenbydescending-2(system-linq-iorderedasyncenumerable((-0))-system-func((-0-1))-system-collections-generic-icomparer((-1)))
///
/// Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An ordered async-enumerable sequence that contains elements to sort.
/// A function to extract a key from each element.
/// A comparer to compare keys.
/// An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.
/// or is null.
public static IOrderedAsyncEnumerable ThenByDescending(this IOrderedAsyncEnumerable source, Func keySelector, IComparer comparer)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
}
#endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
///
/// Performs a subsequent ordering of the elements in a sequence in descending order by using a specified comparer. The keys are obtained by invoking a transform function on each element and awaiting the result.
///
/// The type of the elements of source.
/// The type of the key returned by keySelector.
/// An ordered async-enumerable sequence that contains elements to sort.
/// An asynchronous function to extract a key from each element.
/// A comparer to compare keys.
/// An ordered async-enumerable sequence whose elements are sorted in descending order according to a key.
/// or is null.
[GenerateAsyncOverload]
[Obsolete("Use ThenByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByDescendingAwait functionality now exists as overloads of ThenByDescending.")]
private static IOrderedAsyncEnumerable ThenByDescendingAwaitCore(this IOrderedAsyncEnumerable source, Func> keySelector, IComparer comparer)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
}
#if !NO_DEEP_CANCELLATION
[GenerateAsyncOverload]
[Obsolete("Use ThenByDescending. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the ThenByDescendingAwaitWithCancellation functionality now exists as overloads of ThenByDescending.")]
private static IOrderedAsyncEnumerable ThenByDescendingAwaitWithCancellationCore(this IOrderedAsyncEnumerable source, Func> keySelector, IComparer comparer)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
return source.CreateOrderedEnumerable(keySelector, comparer, descending: true);
}
#endif
}
}