Table of Contents

Method MaxBy

Namespace
Gamelogic.Extensions.Algorithms
Assembly
Gamelogic.Extensions.dll

MaxBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>)

Finds the maximum element in the source as scored by its projection.

public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector)

Parameters

source IEnumerable<TSource>

The source to find the maximum in.

selector Func<TSource, TKey>

A function to extract a comparable key from each element.

Returns

TSource

The element with the largest key.

Type Parameters

TSource

The type of elements in the source.

TKey

The type of the key used to compare elements.

Exceptions

InvalidOperationException

The sequence is empty.

MaxBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>)

Finds the maximum element in the source as scored by the given function applied to a projection on the elements.

public static TSource MaxBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector, IComparer<TKey> comparer)

Parameters

source IEnumerable<TSource>

The source to find the maximum in.

selector Func<TSource, TKey>

A function to extract a comparable key from each element.

comparer IComparer<TKey>

The comparer to use when comparing keys.

Returns

TSource

The element with the largest key.

Type Parameters

TSource

The type of elements in the source.

TKey

The type of the key used to compare elements.

Exceptions

InvalidOperationException

The sequence is empty.