score:4

Accepted answer

well, if you wanted to, you could of course write an indexofmaxbyextension yourself.

example(untested):

public static int indexofmaxby<tsource, tprojected>
    (this ienumerable<tsource> source,
     func<tsource, tprojected> selector,
     icomparer<tprojected> comparer = null
    )
{

    //null-checks here

    using (var erator = source.getenumerator())
    {
        if (!erator.movenext())
            throw new invalidoperationexception("sequence is empty.");

        if (comparer == null)
            comparer = comparer<tprojected>.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 indexofpointwithhighestitem2 = mylist.indexofmaxby(x => x.item2);

score:5

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);

Related Query

More Query from same tag