SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method PartialSortBy

    | Edit this page

    PartialSortBy<TSource, TKey>(IEnumerable<TSource>, int, Func<TSource, TKey>)

    Executes a partial sort of the top count elements of a sequence according to the key for each element. If count is less than the total number of elements in source, then this method will improve performance.

    Declaration
    public static IEnumerable<TSource> PartialSortBy<TSource, TKey>(this IEnumerable<TSource> source, int count, Func<TSource, TKey> keySelector)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    int count

    Number of (maximum) elements to return.

    Func<TSource, TKey> keySelector

    A function to extract a key from an element.

    Returns
    Type Description
    IEnumerable<TSource>

    A sequence containing at most top count elements from source, in ascending order of their keys.

    Type Parameters
    Name Description
    TSource

    Type of elements in the sequence.

    TKey

    Type of keys.

    Remarks

    This operation is an O(n * log(K)) where K is count.

    This operator uses deferred execution and streams it results.

    Examples

    The following code example demonstrates how to get the top N items of a sequence using PartialSort.

    var sequence = new[]
    {
        (key: 5, text: "1"),
        (key: 5, text: "2"),
        (key: 4, text: "3"),
        (key: 4, text: "4"),
        (key: 3, text: "5"),
        (key: 3, text: "6"),
        (key: 2, text: "7"),
        (key: 2, text: "8"),
        (key: 1, text: "9"),
        (key: 1, text: "10"),
    };
    
    // Get the top N items
    var result = sequence
        .PartialSortBy(
            3,
            x => x.key);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [(1, 9), (1, 10), (2, 7)]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or keySelector is null.

    ArgumentOutOfRangeException

    count is less than 1.

    | Edit this page

    PartialSortBy<TSource, TKey>(IEnumerable<TSource>, int, Func<TSource, TKey>, OrderByDirection)

    Executes a direction partial sort of the top count elements of a sequence according to the key for each element. If count is less than the total number of elements in source, then this method will improve performance.

    Declaration
    public static IEnumerable<TSource> PartialSortBy<TSource, TKey>(this IEnumerable<TSource> source, int count, Func<TSource, TKey> keySelector, OrderByDirection direction)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    int count

    Number of (maximum) elements to return.

    Func<TSource, TKey> keySelector

    A function to extract a key from an element.

    OrderByDirection direction

    The direction in which to sort the elements

    Returns
    Type Description
    IEnumerable<TSource>

    A sequence containing at most top count elements from source, in the specified order of their keys.

    Type Parameters
    Name Description
    TSource

    Type of elements in the sequence.

    TKey

    Type of keys.

    Remarks

    This operation is an O(n * log(K)) where K is count.

    This operator uses deferred execution and streams it results.

    Examples

    The following code example demonstrates how to get the top N items of a sequence using PartialSort.

    var sequence = new[]
    {
        (key: 5, text: "1"),
        (key: 5, text: "2"),
        (key: 4, text: "3"),
        (key: 4, text: "4"),
        (key: 3, text: "5"),
        (key: 3, text: "6"),
        (key: 2, text: "7"),
        (key: 2, text: "8"),
        (key: 1, text: "9"),
        (key: 1, text: "10"),
    };
    
    // Get the top N items
    var result = sequence
        .PartialSortBy(
            3,
            x => x.key,
            OrderByDirection.Descending);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [(5, 1), (5, 2), (4, 3)]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or keySelector is null.

    ArgumentOutOfRangeException

    count is less than 1.

    | Edit this page

    PartialSortBy<TSource, TKey>(IEnumerable<TSource>, int, Func<TSource, TKey>, IComparer<TKey>?)

    Executes a partial sort of the top count elements of a sequence according to the key for each element, using comparer to compare the keys. If count is less than the total number of elements in source, then this method will improve performance.

    Declaration
    public static IEnumerable<TSource> PartialSortBy<TSource, TKey>(this IEnumerable<TSource> source, int count, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    int count

    Number of (maximum) elements to return.

    Func<TSource, TKey> keySelector

    A function to extract a key from an element.

    IComparer<TKey> comparer

    A IComparer<T> to compare elements.

    Returns
    Type Description
    IEnumerable<TSource>

    A sequence containing at most top count elements from source, in ascending order of their keys.

    Type Parameters
    Name Description
    TSource

    Type of elements in the sequence.

    TKey

    Type of keys.

    Remarks

    This operation is an O(n * log(K)) where K is count.

    This operator uses deferred execution and streams it results.

    Examples

    The following code example demonstrates how to get the top N items of a sequence using PartialSort.

    var sequence = new[]
    {
        (key: 5, text: "1"),
        (key: 5, text: "2"),
        (key: 4, text: "3"),
        (key: 4, text: "4"),
        (key: 3, text: "5"),
        (key: 3, text: "6"),
        (key: 2, text: "7"),
        (key: 2, text: "8"),
        (key: 1, text: "9"),
        (key: 1, text: "10"),
    };
    
    // Get the top N items
    var result = sequence
        .PartialSortBy(
            1,
            x => x.key,
            Comparer<int>.Create((x, y) => (x % 2).CompareTo(y % 2)));
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [(4, 3)]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or keySelector is null.

    ArgumentOutOfRangeException

    count is less than 1.

    | Edit this page

    PartialSortBy<TSource, TKey>(IEnumerable<TSource>, int, Func<TSource, TKey>, IComparer<TKey>?, OrderByDirection)

    Executes a direction partial sort of the top count elements of a sequence according to the key for each element, using comparer to compare the keys. If count is less than the total number of elements in source, then this method will improve performance.

    Declaration
    public static IEnumerable<TSource> PartialSortBy<TSource, TKey>(this IEnumerable<TSource> source, int count, Func<TSource, TKey> keySelector, IComparer<TKey>? comparer, OrderByDirection direction)
    Parameters
    Type Name Description
    IEnumerable<TSource> source

    The source sequence.

    int count

    Number of (maximum) elements to return.

    Func<TSource, TKey> keySelector

    A function to extract a key from an element.

    IComparer<TKey> comparer

    A IComparer<T> to compare elements.

    OrderByDirection direction

    The direction in which to sort the elements

    Returns
    Type Description
    IEnumerable<TSource>

    A sequence containing at most top count elements from source, in the specified order of their keys.

    Type Parameters
    Name Description
    TSource

    Type of elements in the sequence.

    TKey

    Type of keys.

    Remarks

    This operation is an O(n * log(K)) where K is count.

    This operator uses deferred execution and streams it results.

    Examples

    The following code example demonstrates how to get the top N items of a sequence using PartialSort.

    var sequence = new[]
    {
        (key: 5, text: "1"),
        (key: 5, text: "2"),
        (key: 4, text: "3"),
        (key: 4, text: "4"),
        (key: 3, text: "5"),
        (key: 3, text: "6"),
        (key: 2, text: "7"),
        (key: 2, text: "8"),
        (key: 1, text: "9"),
        (key: 1, text: "10"),
    };
    
    // Get the top N items
    var result = sequence
        .PartialSortBy(
            1,
            x => x.key,
            Comparer<int>.Create((x, y) => (x % 2).CompareTo(y % 2)),
            OrderByDirection.Descending);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [(5, 1), (5, 2), (3, 5), (3, 6), (1, 9), (1, 10)]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or keySelector is null.

    ArgumentOutOfRangeException

    count is less than 1.

    © SuperLinq Authors. All rights reserved.