1. We are not accepting any new account registrations at this time.

One Func for Ascending & Descending

Discussion in 'News, Freeware, Open Source and Discussions' started by GeoN, Apr 22, 2019.

  1. GeoN

    GeoN Registered User Reverser

    Joined:
    Jul 18, 2017
    Messages:
    109
    Likes Received:
    1,850
    Code:
            //
            // Summary:
            //     Sorts the elements of a sequence in ascending or descending order according to a key.
            //
            // Parameters:
            //   source:
            //     A sequence of values to order.
            //
            //   keySelector:
            //     A function to extract a key from an element.
            //
            //   ascendingOrder:
            //     If true ascending order else descending order.
            //
            // Type parameters:
            //   TSource:
            //     The type of the elements of source.
            //
            //   TKey:
            //     The type of the key returned by keySelector.
            //
            // Returns:
            //     An System.Linq.IOrderedEnumerable`1 whose elements are sorted according to a
            //     key.
            //
            // Exceptions:
            //   T:System.ArgumentNullException:
            //     source or keySelector is null.
            public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, bool ascendingOrder = true)
                => ascendingOrder ? Enumerable.OrderBy(source, keySelector) : source.OrderByDescending(keySelector);