SuperLinq SuperLinq
SuperLinq SuperLinq
DocFX + Singulink = ♥

Search Results for

    Method OrderBy

    | Edit this page

    OrderBy<T, TKey>(IEnumerable<T>, Func<T, TKey>, OrderByDirection)

    Sorts the elements of a sequence in a particular direction (ascending, descending) according to a key

    Declaration
    public static IOrderedEnumerable<T> OrderBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector, OrderByDirection direction)
    Parameters
    Type Name Description
    IEnumerable<T> source

    The sequence to order

    Func<T, TKey> keySelector

    A key selector function

    OrderByDirection direction

    A direction in which to order the elements (ascending, descending)

    Returns
    Type Description
    IOrderedEnumerable<T>

    An ordered copy of the source sequence

    Type Parameters
    Name Description
    T

    The type of the elements in the source sequence

    TKey

    The type of the key used to order elements

    Remarks

    This method is implemented by using deferred execution. However, source will be consumed in it's entirety immediately when first element of the returned sequence is consumed.

    Examples

    The following code example demonstrates how to sort a sequence using OrderBy and ThenBy.

    var fruits = new[]
    {
        "grape", "passionfruit", "banana", "Mango",
        "Orange", "raspberry", "apple", "blueberry",
    };
    
    // Sort the strings first by their length and then
    // alphabetically by passing the identity selector function.
    var result = fruits
        .OrderBy(fruit => fruit.Length)
        .ThenBy(fruit => fruit);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [apple, grape, Mango, banana, Orange, blueberry, raspberry, passionfruit]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or keySelector is null.

    | Edit this page

    OrderBy<T, TKey>(IEnumerable<T>, Func<T, TKey>, IComparer<TKey>?, OrderByDirection)

    Sorts the elements of a sequence in a particular direction (ascending, descending) according to a key

    Declaration
    public static IOrderedEnumerable<T> OrderBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector, IComparer<TKey>? comparer, OrderByDirection direction)
    Parameters
    Type Name Description
    IEnumerable<T> source

    The sequence to order

    Func<T, TKey> keySelector

    A key selector function

    IComparer<TKey> comparer

    An IComparer<T> to compare keys

    OrderByDirection direction

    A direction in which to order the elements (ascending, descending)

    Returns
    Type Description
    IOrderedEnumerable<T>

    An ordered copy of the source sequence

    Type Parameters
    Name Description
    T

    The type of the elements in the source sequence

    TKey

    The type of the key used to order elements

    Remarks

    This method is implemented by using deferred execution. However, source will be consumed in it's entirety immediately when first element of the returned sequence is consumed.

    Examples

    The following code example demonstrates how to sort a sequence using OrderBy and ThenBy.

    var fruits = new[]
    {
        "grape", "passionfruit", "banana", "Mango",
        "Orange", "raspberry", "apple", "blueberry",
    };
    
    // Sort the strings first by their length and then
    // alphabetically by passing the identity selector function.
    var result = fruits
        .OrderBy(fruit => fruit.Length)
        .ThenBy(fruit => fruit, StringComparer.Ordinal);
    
    Console.WriteLine(
        "[" +
        string.Join(", ", result) +
        "]");
    
    // This code produces the following output:
    // [Mango, apple, grape, Orange, banana, blueberry, raspberry, passionfruit]
    
    Exceptions
    Type Condition
    ArgumentNullException

    source or keySelector is null.

    © SuperLinq Authors. All rights reserved.