I have an unordered list of Points (List<Point>
) and I want to find the first Point in the list when ordering by X and then Y.
NOTE: I don't want to actually change the order of the items in the List.
I have an unordered list of Points (List<Point>
) and I want to find the first Point in the list when ordering by X and then Y.
NOTE: I don't want to actually change the order of the items in the List.
This won't change the ordering of the original list, but will sort the resulting enumeration of the list and select the first point after the ordering. It handles the empty list case by returning a default (null) Point.
var firstPoint = Points.OrderBy( p => p.X ).ThenBy( p => p.Y ).FirstOrDefault();
var firstPoint = (from p in Points orderby p.X, p.Y select p).FirstOrDefault();