This is what you can do using System.Linq:
var value = arrayOfThings .OrderByDescending(x => somefunction(x.property, localvariable)) .First();
If the array may be empty, use .FirstOrDefault(); to avoid exceptions.
You really don't know how this is implemented internally, so you cannot guarantee that it will sort the entire array to get the first element. For example, if it was linq for sql, the server would receive a request including sorting and condition. It will not receive the array, then it will sort it, and then will receive the first element.
In fact, until you call First, the first part of the request will not be evaluated. I mean, this is not a two-step assessment, but a one-step assessment.
var sortedValues =arrayOfThings .OrderByDescending(x => somefunction(x.property, localvariable));
Jotabe
source share