compareTo
compareTo is a method used in programming to determine the relative ordering of two objects. In Java, it is defined by the Comparable interface as int compareTo(T o). The contract states that the method returns a negative integer if this object is less than the specified object, zero if they are equal, and a positive integer if this object is greater.
Implementations should define a natural order for a class. The result of compareTo is used by sorting
A key principle is that compareTo is related to equality but not identical. If compareTo returns zero,
Common implementations include lexicographic comparison for strings (based on Unicode values) and numeric comparison for wrapper
Pitfalls include violating the transitivity or antisymmetry requirements, which can break sorted collections. Null handling varies