// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 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
{
///
/// 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);
internal static IOrderedAsyncEnumerable OrderByAwaitCore(this IAsyncEnumerable source, Func> keySelector) =>
new OrderedAsyncEnumerableWithTask(source, keySelector, comparer: null, descending: false, parent: null);
#if !NO_DEEP_CANCELLATION
internal static IOrderedAsyncEnumerable OrderByAwaitWithCancellationCore(this IAsyncEnumerable source, Func> keySelector) =>
new OrderedAsyncEnumerableWithTaskAndCancellation(source, keySelector, comparer: null, descending: false, parent: null);
#endif
///
/// 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);
internal static IOrderedAsyncEnumerable OrderByAwaitCore(this IAsyncEnumerable source, Func> keySelector, IComparer comparer) =>
new OrderedAsyncEnumerableWithTask(source, keySelector, comparer, descending: false, parent: null);
#if !NO_DEEP_CANCELLATION
internal static IOrderedAsyncEnumerable OrderByAwaitWithCancellationCore(this IAsyncEnumerable source, Func> keySelector, IComparer comparer) =>
new OrderedAsyncEnumerableWithTaskAndCancellation(source, keySelector, comparer, descending: false, parent: null);
#endif
///
/// 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);
internal static IOrderedAsyncEnumerable OrderByDescendingAwaitCore(this IAsyncEnumerable source, Func> keySelector) =>
new OrderedAsyncEnumerableWithTask(source, keySelector, comparer: null, descending: true, parent: null);
#if !NO_DEEP_CANCELLATION
internal static IOrderedAsyncEnumerable OrderByDescendingAwaitWithCancellationCore(this IAsyncEnumerable source, Func> keySelector) =>
new OrderedAsyncEnumerableWithTaskAndCancellation(source, keySelector, comparer: null, descending: true, parent: null);
#endif
///
/// 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);
internal static IOrderedAsyncEnumerable OrderByDescendingAwaitCore(this IAsyncEnumerable source, Func> keySelector, IComparer comparer) =>
new OrderedAsyncEnumerableWithTask(source, keySelector, comparer, descending: true, parent: null);
#if !NO_DEEP_CANCELLATION
internal static IOrderedAsyncEnumerable OrderByDescendingAwaitWithCancellationCore(this IAsyncEnumerable source, Func> keySelector, IComparer comparer) =>
new OrderedAsyncEnumerableWithTaskAndCancellation(source, keySelector, comparer, descending: true, parent: null);
#endif
///
/// 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);
}
internal 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
internal 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
///
/// 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);
}
internal 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
internal 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
///
/// 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);
}
internal 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
internal 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
///
/// 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);
}
internal 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
internal 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
}
}