Imagine I have
var points = new Point[]
{
new Point(1, 2),
new Point(2, 3)
};
To get the point with the minimum X I could:
var result = points.OrderBy(point => point.X).First();
But for large arrays, I don’t think this is the faster option. There is a faster alternative?
It is better to use
as this eliminates the necessity of sorting this list (i.e., it is
O(n)as opposed to, say,O(n log n)). Further, it’s clearer than usingOrderByandFirst.You could even write an extension method as follows:
Usage:
You can generalize to your needs. The advantage of this is that it avoids potentially walking the list twice.