I have written an extension function:
public static TSource MaxBy<TSource>(this IEnumerable<TSource> sequence, params Func<TSource, float>[] selectors)
{
for (var i = 0; i < selectors.Length - 1; i++)
{
var indexed = sequence.Select(x => (element: x, value: selectors[i](x)));
sequence = indexed.Where(x => x.value == indexed.Max(y => y.value)).Select(x => x.element);
//`sequence` contains: 18, 25
}
//`sequence` contains: 75
//Last function
if (selectors.Length > 0)
return sequence.MaxBy(selectors[selectors.Length - 1]);
else
throw new ArgumentOutOfRangeException();
}
However, this function does not work as intended. The value of sequence
unintentionally changes between the end of the last iteration of the loop, and after loop exit. What is causing this behavior and how to deal with it?
Tried var temp = sequence
but it also does not help.
New contributor
rus9384 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.