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
6
votes
2 answers

Why is it possible to call method on Java interface method? [Comparable]

In AP Computer Science class today, I had this code: Comparable x = 45; Comparable y = 56; System.out.println(x.compareTo(y)); And this is valid. It prints 1 (or -1, I forget which), but it is possible to compare them. I understand…
Alex Ozer
  • 459
  • 5
  • 14
6
votes
2 answers

understanding comparable mixin and enumerable mixin

I am a newbie and learning ruby. Would like to have a better understanding of the question asked. I don't understand the use of comparable mixin and enumerable mixin. I mean we don't include these in our class when we need to use them, right? if we…
Akash Soti
  • 583
  • 2
  • 7
  • 13
6
votes
1 answer

Java Syntax for a list of comparable objects

I'm writing a method that takes as its only parameter a list of comparable objects and doesn't return anything. I am uncertain as to the syntax that it should have: public static void methodName(List> list) { // Do some…
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
6
votes
2 answers

Java generics with multiple parameters

I have seen examples on the site that deal with generics with multiple parameters but none that work for my situation. So here is the deal: I am trying to learn Java generics and have decided to create a simple binary array search utility function. …
jjNford
  • 5,170
  • 7
  • 40
  • 64
5
votes
3 answers

Generic java class that stores comparables

I have a generic java class that stores comparables: public class MyGenericStorage> { private T value; public MyGenericStorage(T value) { this.value = value; } //... methods that use…
avf
  • 858
  • 11
  • 24
5
votes
2 answers

Scala generics: Int not conforming to Comparable?

The following Scala declarations are OK: trait Base[B <: Base[B,M,ID], M <: Meta[B,M,ID], ID <: Comparable[ID]] { // ... } trait Meta[B <: Base[B,M,ID], M <: Meta[B,M,ID], ID <: Comparable[ID]] extends Ordered[Meta[_,_,_]] { // ... } trait…
Sebastien Diot
  • 7,183
  • 6
  • 43
  • 85
5
votes
3 answers

Why can Collections.sort take no comparator but List.sort must take a comparator?

I don't understand the logic why List.sort() doesn't have a version with no comparator. Specially I see it's possible to call List.sort with null: list.sort(null), and it seems it uses the natural order for sorting. I noticed in my IDE…
apadana
  • 13,456
  • 15
  • 82
  • 98
5
votes
2 answers

Why doesn't IComparable inherit from IComparable?

In most places I read that it is a good idea to inherit from both IComparable and IComparable in your classes to provide compatibility with non-generic collections. My question is why IComparable doesn't inherit from IComparable by default…
5
votes
2 answers

Why LocalDate doesn't implements Comparable?

Initial Problem: In Scala, I would like to use implicit Ordering[T]#Ops to compare two LocalDate. It just to use "operators" like > instead of isAfter. It should be just an import: import scala.math.Ordering.Implicits._ Inspection: Looks like it…
mkUltra
  • 2,828
  • 1
  • 22
  • 47
5
votes
1 answer

What does it mean by saying "Comparable affects the original class but Comparator doesnt"

// Original class Dog class Dog{ String name; int age; } //Case 1 class Dog implements Comparable{ //compareTo() implementation } //Case2 class Dog implements Comparator{ // compare()…
5
votes
5 answers

implementing Comparable in an interface

I am calling a specific class using only its interface. The problem is, the class itself implements Comparable, but because I am referring to the class via a different interface, the compiler does not know it implements Comparable. I'm sure there is…
Ben B.
  • 3,706
  • 5
  • 26
  • 27
5
votes
1 answer

Using reference variable to point to a Comparator?

I ran into a new way of creating Comparator while doing exercises. Can anyone explain this a little bit? class Checker{ public Comparator desc = new Comparator() { public int compare(Player A, Player B){ …
whales
  • 787
  • 1
  • 12
  • 24
5
votes
2 answers

How to fix a stack overflow Error in java?

I have a class Movie with a static array Movie[] movies. I implement the Comparable and i overload the method compareTo. If the likes of a movie are same with the likes of another movie then i compare them with alphabetical order. I have to create a…
Damis Berzovitis
  • 205
  • 1
  • 3
  • 11
5
votes
2 answers

Why have a cast in a compareTo(Object)

public int compareTo(Object x) { Task other = (Task)x; if (other.priority == this.priority) { System.out.println("Each task has the same priority level."); return 0; } else if (other.priority > this.priority) { …
Sarah Diri
  • 97
  • 1
  • 9
5
votes
1 answer

How does the sort() method of the Collection class call the Comparable's compareTo()?

Suppose I want to sort a list of Employee objects: Employee emp1 = new Employee("Abhijit", 10); Employee emp2 = new Employee("Aniket", 5); Employee emp3 = new Employee("Chirag", 15); List employees = new…