Max component of a Vector

I have an array of Vector3s, and I need to find the Vector3 with the highest Y value. For example, if I have the following Vector3s

Vector3[] maxvalue = new Vector3[3];
maxvalue[0] = [1, 5, 10];
maxvalue[1] = [1, 3, 12];
maxvalue[2] = [1 ,17, 4];

Then it would return maxvalue[2]. Also, if for the above code there was another Vector3 with the value [1, 17, 23], it would need to return that as well since it has the maximum value as well.

I tried using maxvalue.y.Max(); to take advantage of the Max() function from Linq, but it would not work. Any suggestions? I need this because I have an array of Vector3s which store the vertices from an object, and I need to pull out the vertices with the highest and lowest Y values.

It’s possible to use Linq to sort the collection and just grab the first / last (depending on your sort equation). However i would not recommend it since to sort the whole collection Linq need to create a temporary copy of the whole collection. It’s better for performance to do it the “manual” way. Here’s an extention method that can returns any extrem value you want:

public static class Vector3ArrayExtension
{
    public delegate bool OnCompareExtremum(Vector3 aRef, Vector3 aValue);
    public static Vector3 GetExtremum(this Vector3[] aArray, OnCompareExtremum aCallback)
    {
        if (aArray == null || aArray.Length == 0)
            throw new System.ArgumentException("array is null or empty");
        Vector3 v = aArray[0];
        for (int i = 1; i < aArray.Length; i++)
        {
            if (aCallback(v, aArray*))*

{
v = aArray*;*
}
}
return v;
}
}
Some examples using your array above:
Vector3 max_y = maxvalue.GetExtremum( (r, v) => v.y > r.y );
Vector3 max_z = maxvalue.GetExtremum( (r, v) => v.z > r.z );
Vector3 min_z = maxvalue.GetExtremum( (r, v) => v.z < r.z );
As you can see the delegate has two parameters, a reference value and the current checked value. When the delegate returns “true” the current value will become the new reference value. So the delegate can be used to select if you want a min or max value and you can choose which Vector3 component you want to compare.