Skip to content

Instantly share code, notes, and snippets.

@chuongmep
Created March 8, 2023 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuongmep/bd0c676bd5330ca91203b53f897425c4 to your computer and use it in GitHub Desktop.
Save chuongmep/bd0c676bd5330ca91203b53f897425c4 to your computer and use it in GitHub Desktop.
void Main()
{
double x1 = 10;
double y1 = 10;
double z1 = 10;
double x2 = 10;
double y2 = 10;
double z2 = 10;
var point1 = new ComparePoint(x1,y1,z1);
var point2 = new ComparePoint(x2,y2,z2);
// case equal
var result = point1.CompareTo(point2);
// if equal return 0
// if less than return negative value
// if greater than return positive value
Console.WriteLine(result);
}
public class ComparePoint : IComparable<ComparePoint>
{
public double x { get; }
public double y { get; }
public double z { get; }
public ComparePoint(double x, double y, double z)
{
this.x = x;
this.y = y;
this.z = z;
}
public int CompareTo(ComparePoint other)
{
if (other == null) return 1;
if (x.CompareTo(other.x) != 0)
{
return x.CompareTo(other.x);
}
else if (y.CompareTo(other.y) != 0)
{
return y.CompareTo(other.y);
}
else if (z.CompareTo(other.z) != 0)
{
return z.CompareTo(other.z);
}
else
{
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment