Hi
What does "min point" mean ?
In a collection containing 2 point : { (1, 0), (0, 1) }, which is the "min point" ?
For this collection, Daniel's method should return (0, 0) as "min point" which does not belong to the collection.
I think you have to define more precisely the criteria used for "min point" and this criteria can be use with the IEnumerable .OrderBy() Linq extension method as Kerry said or the List<T>.Sort().
But as you don't have to sort the whole collection, the Linq Aggregate() extension method should be faster.
Example to get the "min point" comparing X coordinate first, then Y in case of equality, then Z.
private Point3d MinPoint(IEnumerable<Point3d> pts)
{
return pts.Aggregate((p1, p2) =>
{
if (p1.X == p2.X)
{
if (p1.Y == p2.Y)
{
if (p1.Z < p2.Z) return p1;
else return p2;
}
else if (p1.Y < p2.Y) return p1;
else return p2;
}
else if (p1.X < p2.X) return p1;
else return p2;
});
}
If you're not comfortable with lambda expressions, you can define a separated method for the accumulator
Exemple comaparing the coordinates sum first, then X then Y coordinate
private Point3d ComparePoints(Point3d p1, Point3d p2)
{
double s1 = p1.X + p1.Y + p1.Z;
double s2 = p2.X + p2.Y + p2.Z;
if (s1 == s2)
{
if (p1.X == p2.X)
{
if (p1.Y < p2.Y) return p1;
else return p2;
}
else if (p1.X < p2.X) return p1;
else return p2;
}
else if (s1 < s2) return p1;
else return p2;
}
...
Point3d minPt = pts.Aggregate(ComparePoints)