Questions tagged [comparable]

In Java, this interface is implemented by a class to indicate that it can be compared to another object and therefore ordered. In Go, comparable is a predeclared interface constraint for generic types that support == and != operators. Make sure to use the appropriate language tag along with this.

In Java, this interface is implemented by a class to indicate that it can be compared to another object and therefore ordered. The interface defines one method: compareTo(). Classes can either implement it as a raw type, as with older versions of Java:

public class MyClass implements Comparable
{
    public int compareTo(Object other)
    {
        MyClass toCompare = (MyClass) other;
        //...
    }
}

Or, as of Java 1.5, a class can use generics:

public class MyClass implements Comparable<MyClass>
{
    public int compareTo(MyClass other)
    {
        //...
    }
}

The general contract of compareTo() is:

  • If this is less than the parameter, it returns a value less than zero.
  • If this is greater than the parameter, it returns a value greater than zero.
  • Otherwise, if this is equal to the parameter, returns zero.

The actual value of compareTo() is irrelevant; only the sign of the return value matters.

Javadoc for java.lang.Comparable


In Go, comparable is a predeclared interface constraint. It can be used directly or it can be embedded in other interface constraints. It cannot be used in union terms. Specifications: Type Constraints:

The predeclared interface type comparable denotes the set of all concrete (non-interface) types that are comparable. Specifically, a type T implements comparable if:

  • T is not an interface type and T supports the operations == and !=; or
  • T is an interface type and each type in T's type set implements comparable.

Example usage:

func IsZero[T comparable](v T) bool {
    return v == *new(T)
}
1251 questions
5
votes
4 answers

Java - Implementing Interfaces

I am working on a homework assignment for my into to programming class that involves implementing interfaces. The problem here is that I really just flat out don't understand interfaces or what they are used for (the professor was not very good…
salxander
  • 1,081
  • 5
  • 20
  • 29
4
votes
4 answers

compareTo() method is not overriding default method when using Comparable interface

I am trying to overwrite the default compareTo() method in java by writing my own and using implements comparable, however it seems that java is still using the default method. I am trying to sort an array of Strings by length that I get from a .dat…
Special K
  • 41
  • 1
  • 1
  • 2
4
votes
3 answers

Java Comparable return value, does it have to be exactly 1, -1 or 0?

This might be a trivial question, but I have not found anything about it, so here goes: When implementing the Comparable interface, we are supposed to define the method compareTo(), so that the following is true according to the…
Jave
  • 31,598
  • 14
  • 77
  • 90
4
votes
1 answer

Sort unbound Comparable in Scala

I am somewhat familiar with sorting in Scala using Ordering's, however I would like to sort some objects which are defined in Java. They are Comparable (not Comparable[T]) and final: final class Term implements Comparable { ... } (this is actually…
jpg
  • 43
  • 3
4
votes
1 answer

Go generics: type constraint for map keys?

In the code below, I define a generic linked list. Go1.18 is happy to use an instance of the list as a key to a map. However, the last line, when uncommented, doesn't compile; I get the error: Cons[int] does not implement comparable Is there a…
Mike Stay
  • 1,071
  • 8
  • 17
4
votes
3 answers

Best practice for compareTo() when argument must be typed of super class

I'm looking for best practice for the definition of the compareTo() method in the case where the class implements Comparable. Thus, the signature of the method has to be public int compareTo(BaseClass arg) The obvious thing to do first is check if…
fishtoprecords
  • 2,394
  • 7
  • 27
  • 38
4
votes
4 answers

Java: unchecked call to compareTo(T)

1 class test { 2 public static int compare0(Comparable x, Comparable y) { 3 return x.compareTo(y); 4 } 5 public static int compare1(Object x, Object y) { 6 return ((Comparable) x).compareTo((Comparable) y); …
kjo
  • 33,683
  • 52
  • 148
  • 265
4
votes
1 answer

What might cause Collections.sort(List, Comparator) to throw a ClassCastException?

I am calling Collections.sort() on an ArrayList using a Comparator that I declared earlier. ArrayList list = new ArrayList(); Comparator comparator = new Comparator() { public int compare(Employee o1,…
peskal
  • 1,213
  • 1
  • 12
  • 28
4
votes
2 answers

Would it be possible to add default methods to Comparable without breaking Java?

I was thinking of proposing a feature request to add default methods called: default boolean greaterThan(T o) { return compareTo(o) > 0; } default boolean smallerThan(T o) { return compareTo(o) < 0; } default boolean atLeast(T o) { …
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
4
votes
2 answers

Sort List> based on value

Basically I have a List>, and I want to sort it by the values of certain key in the map. The problem is that I do not know the type... This map can contain Strings, Integers, Doubles, Floats etc.... I would like to sort it: So…
Vml11
  • 361
  • 1
  • 2
  • 11
4
votes
1 answer

Java: Comparable vs Comparator - Memory & Performance

During one of my interview, I was asked What is the performance difference between Comparable and Comparator? I replied that I don't know. The interviewer said, If Comparable is implemented by a class Employee, when 5000 Employee objects…
gokul
  • 85
  • 11
4
votes
1 answer

How can I know the sort order of a class implementing Comparable<> without actually running the code?

I have the following example and I would like to know if there is a way to tell in which order, ascending or descending, the movie arrray gets sorted just by looking at the compareTo() method WITHOUT running the code and doing trial and error. The…
Fabian Andiel
  • 321
  • 1
  • 13
4
votes
1 answer

What is the difference between these two compareTo methods?

I want to understand what is the difference between these two methods? Can they both be used for the same problem? Or are they designed for different cases? public int compareTo(Coordinates o) { if (row < o.row) return -1; if (row > o.row)…
thpthp
  • 93
  • 1
  • 3
4
votes
3 answers

Comparable VS

regarding the following code: public class Test { public static void main(String[] args){ List lst = Array.asList("abc","def"); System.out.println(func(lst)); } public static boolean…
Kinor
  • 55
  • 4
4
votes
3 answers

Does compareTo have some sort of pre-launching delay?

I just found this statement: "One can greatly increase the performance of compareTo by comparing first on items which are most likely to differ". Is it true? And if it is, why?
dhblah
  • 9,751
  • 12
  • 56
  • 92