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

Java : Comparable vs Comparator

Possible Duplicates: difference between compare() and compareTo() Java: What is the difference between implementing Comparable and Comparator? What are the keys differences between Comparable and Comparator. and which is preferred over the…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
152
votes
12 answers

Why doesn't java.lang.Number implement Comparable?

Does anyone know why java.lang.Number does not implement Comparable? This means that you cannot sort Numbers with Collections.sort which seems to me a little strange. Post discussion update: Thanks for all the helpful responses. I ended up doing…
Julien Chastang
  • 17,592
  • 12
  • 63
  • 89
146
votes
11 answers

When should a class be Comparable and/or Comparator?

I have seen classes which implement both Comparable and Comparator. What does this mean? Why would I use one over the other?
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
123
votes
19 answers

When to use Comparable and Comparator

I have a list of objects I need to sort on a field, say Score. Without giving much thought I wrote a new class that implements Comparator, that does the task and it works. Now looking back at this, I am wondering if I should have instead have the…
pkrish
  • 2,229
  • 7
  • 22
  • 29
114
votes
9 answers

How to implement the Java comparable interface?

I am not sure how to implement a comparable interface into my abstract class. I have the following example code that I am using to try and get my head around it: public class Animal{ public String name; public int yearDiscovered; public…
Softey
  • 1,451
  • 3
  • 21
  • 42
104
votes
5 answers

Why is compareTo on an Enum final in Java?

An enum in Java implements the Comparable interface. It would have been nice to override Comparable's compareTo method, but here it's marked as final. The default natural order on Enum's compareTo is the listed order. Does anyone know why a Java…
neu242
  • 15,796
  • 20
  • 79
  • 114
99
votes
3 answers

How to sort based on/compare multiple values in Kotlin?

Say I have a class Foo(val a: String, val b: Int, val c: Date) and I want to sort a list of Foos based on all three properties. How would I go about this?
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
87
votes
7 answers

What do the return values of Comparable.compareTo mean in Java?

What is the difference between returning 0, returning 1 and returning -1 in compareTo() in Java?
Magggi
  • 1,145
  • 2
  • 12
  • 17
73
votes
12 answers

Comparing the values of two generic Numbers

I want to compare to variables, both of type T extends Number. Now I want to know which of the two variables is greater than the other or equal. Unfortunately I don't know the exact type yet, I only know that it will be a subtype of…
b_erb
  • 20,932
  • 8
  • 55
  • 64
61
votes
5 answers

Does a natural comparator exist in the standard api?

I need a comparator as part of a strategy pattern that can either use the natural ordering of the objects or some custom ordering. For the natural ordering case, I wrote a simple comparator: private static class NaturalComparator
Yishai
  • 90,445
  • 31
  • 189
  • 263
56
votes
7 answers

Sorting Java objects using multiple keys

I've got a collection of Duck objects and I'd like to sort them using multiple keys. class Duck { DuckAge age; //implements Comparable DuckWeight weight; //implements Comparable String name; } List ducks = Pond.getDucks(); eg. I…
andras
  • 6,339
  • 6
  • 26
  • 22
51
votes
4 answers

Creating exclusive ranges in Kotlin

I am just starting with Kotlin. I want to create a range from 1 to n where n is excluded. I found out that Kotlin has ranges and I can use them as follows: 1..n But this is an inclusive range which includes 1 and n. How do I create exclusive…
Chetan Kothari
  • 761
  • 1
  • 5
  • 12
39
votes
3 answers

Why is Stream.sorted not type-safe in Java 8?

This is from the Stream interface from Oracle's implementation of JDK 8: public interface Stream extends BaseStream> { Stream sorted(); } and it is very easy to blow this up at run time and no warning will be generated at…
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
36
votes
5 answers

Comparable classes in Python 3

What is the standard way of making a class comparable in Python 3? (For example, by id.)
Neil G
  • 32,138
  • 39
  • 156
  • 257
36
votes
3 answers

Comparable and Comparator contract with regards to null

Comparable contract specifies that e.compareTo(null) must throw NullPointerException. From the API: Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
1
2 3
83 84