Created
March 8, 2023 00:30
-
-
Save chuongmep/bd0c676bd5330ca91203b53f897425c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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