How can I get LINQ to return the index of the object which has the max value in a collection?

Well, if you wanted to, you could of course write an IndexOfMaxBy extension yourself Example(untested): public static int IndexOfMaxBy (this IEnumerable source, Func selector, IComparer comparer = null ) { //null-checks here using (var erator = source.GetEnumerator()) { if (!erator.MoveNext()) throw new InvalidOperationException("Sequence is empty. "); if (comparer == null) comparer = Comparer. Default; int index = 0, maxIndex = 0; var maxProjection = selector(erator.

Current); while (erator.MoveNext()) { index++; var projectedItem = selector(erator. Current); if (comparer. Compare(projectedItem, maxProjection) > 0) { maxIndex = index; maxProjection = projectedItem; } } return maxIndex; } } Usage: var indexOfPointWithIndexOfMaxBy(x => x.

Item2).

Well, if you wanted to, you could of course write an IndexOfMaxByextension yourself. Example(untested): public static int IndexOfMaxBy (this IEnumerable source, Func selector, IComparer comparer = null ) { //null-checks here using (var erator = source.GetEnumerator()) { if (!erator.MoveNext()) throw new InvalidOperationException("Sequence is empty. "); if (comparer == null) comparer = Comparer.

Default; int index = 0, maxIndex = 0; var maxProjection = selector(erator. Current); while (erator.MoveNext()) { index++; var projectedItem = selector(erator. Current); if (comparer.

Compare(projectedItem, maxProjection) > 0) { maxIndex = index; maxProjection = projectedItem; } } return maxIndex; } } Usage: var indexOfPointWith IndexOfMaxBy(x => x. Item2).

– Oscar Mederos Mar 4 at 4:24 1 Yes, this is probably the most speed-efficient approach. It would be nice though to share logic in both MaxyBy (see morelinq) and IndexOfMaxBy since they are so similar. – dominic Mar 4 at 4:37.

It looks like there is a FindIndex method defined on List that would be perfect for this: double max = myList. Max(t => t. Item2); int index = myList.

FindIndex(t => t. Item2 == max).

1 because the code is as short and sweet as it gets (since there is no method that directly does what the original question wants), and is still linear time. – Merlyn Morgan-Graham Mar 4 at 4:26 I didn't know about FindIndex, so that's handy to know - thanks. However, this solution uses two passes of the list, it would be nice to use just one.

– dominic Mar 4 at 4:31 2 @dominic: Depending on the size of the lists in question, it may be worth it, simply to avoid having to write boilerplate code. Fewer sources of bugs, and intent is clearly documented :) On average you won't have to go through the list twice, anyhow. The worst case would be a sorted list with the maximum value at the end.

– Merlyn Morgan-Graham Mar 4 at 5:00.

In the end I adopted Ani's approach (I have accepted his answer) with a slight modification, in that I created a function which returned both the object and its index. Thanks for all who contributed suggestions.

Var maxProjection = selector(erator. Var projectedItem = selector(erator. Var indexOfPointWithHighestItem2 = myList.

IndexOfMaxBy(x => x.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions