/// <summary> /// Combines <see cref="MoreEnumerable.OrderBy{T, TKey}(IEnumerable{T}, Func{T, TKey}, IComparer{TKey}, OrderByDirection)"/>, /// where each element is its key, and <see cref="Enumerable.Take{TSource}"/> /// in a single operation. /// Additional parameters specify how the elements compare to each other and /// the direction of the sort. /// </summary> /// <typeparam name="T">Type of elements in the sequence.</typeparam> /// <param name="source">The source sequence.</param> /// <param name="count">Number of (maximum) elements to return.</param> /// <param name="comparer">A <see cref="IComparer{T}"/> to compare elements.</param> /// <param name="direction">The direction in which to sort the elements</param> /// <returns>A sequence containing at most top <paramref name="count"/> /// elements from source, in the specified order.</returns> /// <remarks> /// This operator uses deferred execution and streams it results. /// </remarks> public static IEnumerable <T> PartialSort <T>(this IEnumerable <T> source, int count, IComparer <T> comparer, OrderByDirection direction) { comparer = comparer ?? Comparer <T> .Default; if (direction == OrderByDirection.Descending) { comparer = new ReverseComparer <T>(comparer); } return(source.PartialSort(count, comparer)); }
/// <summary> /// Combines <see cref="MoreEnumerable.OrderBy{T, TKey}(IEnumerable{T}, Func{T, TKey}, OrderByDirection)"/>, /// and <see cref="Enumerable.Take{TSource}"/> in a single operation. /// Additional parameters specify how the elements compare to each other and /// the direction of the sort. /// </summary> /// <typeparam name="TSource">Type of elements in the sequence.</typeparam> /// <typeparam name="TKey">Type of keys.</typeparam> /// <param name="source">The source sequence.</param> /// <param name="keySelector">A function to extract a key from an element.</param> /// <param name="count">Number of (maximum) elements to return.</param> /// <param name="comparer">A <see cref="IComparer{T}"/> to compare elements.</param> /// <param name="direction">The direction in which to sort the elements</param> /// <returns>A sequence containing at most top <paramref name="count"/> /// elements from source, in the specified order of their keys.</returns> /// <remarks> /// This operator uses deferred execution and streams it results. /// </remarks> public static IEnumerable <TSource> PartialSortBy <TSource, TKey>( this IEnumerable <TSource> source, int count, Func <TSource, TKey> keySelector, IComparer <TKey> comparer, OrderByDirection direction) { comparer = comparer ?? Comparer <TKey> .Default; if (direction == OrderByDirection.Descending) { comparer = new ReverseComparer <TKey>(comparer); } return(source.PartialSortBy(count, keySelector, comparer)); }