this is how i do to sort and distinct list of XYZ:
Debug.Print("All Points");
i = 0;
foreach (XYZ xyz in lstxyzPoints) {
i++;
Debug.Print(i.ToString() + " - " + xyz.ToString());
}
List<XYZ> lstxyzPointsDistinct = GetDistinctPoints(lstxyzPoints);
Debug.Print("Distinct Points");
i = 0;
foreach (XYZ xyz in lstxyzPointsDistinct) {
i++;
Debug.Print(i.ToString() + " - " + xyz.ToString());
}
private List<XYZ> GetDistinctPoints(List<XYZ> lstxyz) {
List<XYZ> lstxyzRound = new List<XYZ>();
int i = 1, j = 1;
//round the points to 6 decimal places (you may not need this)
foreach (XYZ xyz in lstxyz) {
lstxyzRound.Add(new XYZ(Math.Round(xyz.X, 6), Math.Round(xyz.Y, 6), Math.Round(xyz.Z, 6)));
}
//order by Z,X,Y (depends on your need)
lstxyzRound = lstxyzRound.OrderBy(p => p.Y).ToList();
lstxyzRound = lstxyzRound.OrderBy(p => p.X).ToList();
lstxyzRound = lstxyzRound.OrderBy(p => p.Z).ToList();
//remove points from list if duplicates
bool blnDuplicate = true;
while (blnDuplicate) {
blnDuplicate = false;
for (i = j; i < lstxyzRound.Count; i++) {
if (lstxyzRound[i - 1].DistanceTo(lstxyzRound[i]) < 0.0001) {
blnDuplicate = true;
j = i;
break;
}
}
if (blnDuplicate) {
lstxyzRound.RemoveAt(j);
}
}
return lstxyzRound;
}