I am just beginning to learn the very basics of C#. I made an arbitrary curve and placed points on it at regular intervals, now I would like to sort these points and connect them with a polyline (such as in attached image). In this case I would like to sort by x… How do I deconstruct a point in C# and get their x components and sort?
Point3d comparison is already based on x coordinate (first x, then y if xs are equal, then z if ys are also equal) so you can just sort them directly:
Point3d[] points = ...;
Array.Sort(points);
However sometimes you want to sort based on different attributes. If that attribute can be captured in a single value, you can use sorting keys. For example, let’s sort an array of points based on their distance to some locus:
Point3d[] points = ...;
Point3d locus = ...;
double[] keys = new double[points.Length];
for (int i = 0; i < points.Length; i++)
keys[i] = points[i].DistanceTo(locus);
Array.Sort(keys, points);
If you really want to go crazy and define your own multivariate sorting logic, then you can use one of the Array.Sort() or List<T>.Sort() overrides which take an IComparer or Comparison<T> argument.