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
4
votes
3 answers

Ensuring that "Object o" parameter is of the same generic type when implementing java interfaces

I am trying to create a hybrid list that uses Binary Search Trees for various functions. The class tries to implement List< E >. To index, these trees use comparison to decide whether data should be placed in the left or right child of the parent.…
jeanluc
  • 1,608
  • 1
  • 14
  • 28
4
votes
3 answers

Java, how to use compareTo to sort an Arraylist

Im trying to figure out how to sort an ArrayList using comparable, my code looks like this: public class playerComparsion{ public static void main(String[] args){ ArrayList list = new ArrayList(); …
user3712130
  • 87
  • 1
  • 3
  • 8
4
votes
2 answers

Comparable not sorting object

I'm having a hard time sorting out my objects according to its properties. I have this list of patient objects that I want to sort according to their last name but it doesn't sort them at all. Can you tell me what am I doing wrong? Here are some…
philip
  • 1,292
  • 3
  • 24
  • 44
4
votes
1 answer

Compiler thinks Comparable type is not Comparable

So I have a class that implements Comparable (I have a dummy method here for brevity) public class MarkovEntry implements Comparable> { // Compare two rows by ID public int compareTo(MarkovEntry e) …
4
votes
3 answers

Difference between Comparable and Object type

Currently am working on an assignment that puts the concepts of Comparable to application. I wrote this simple method that allows the input of a Comparable[] array. This method returns the minimum value of any given array input. Using the…
theGreenCabbage
  • 5,197
  • 19
  • 79
  • 169
4
votes
4 answers

Java comparable interface compareTo(Object o), what is object o?

Beginner, can't seem to wrap my head around this. Item Class public class Item implements Comparable { private int number; private String description; public Item(int num, String descript){ this.number = num; this.description =…
Eric Huang
  • 1,114
  • 3
  • 22
  • 43
4
votes
6 answers

Why use a nested class to implement Comparator?

Going through docjar for String, I happened to see the following piece of code: public static final Comparator CASE_INSENSITIVE_ORDER = new CaseInsensitiveComparator(); private static class…
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
4
votes
3 answers

Sorting ArrayList full of objects by their id using Collections.sort(arrayListName)

I have an inventory class which creates an ArrayList full of objects Item, which is a class as well. I know that I have to call Collections.sort(items); in order to sort the ArrayList (it's called items by the way). The assignment says that I…
bassandguitar
  • 189
  • 2
  • 2
  • 8
4
votes
5 answers

Java Generics: Comparing an Integer to a Double

Can anyone help me compare an Integer to a Double using generics? This is what I have: public static > int compare(T arg1, T arg2) { return arg1.compareTo(arg2); } public static void main(String[] args) { …
karakays
  • 3,643
  • 3
  • 20
  • 14
4
votes
4 answers

How to efficiently compare Sets?

Given two Sets: how to compare them efficiently in Java? (a) keep them as Lists, sort them and compare them. (Comparable) (b) keep them as Sets and compare the hashCode of the Sets? background: many comparisons need to be done Sets are small…
user1654885
  • 131
  • 1
  • 3
  • 8
4
votes
3 answers

Comparable and Comparator Interface in Java

I want to write a generic Pair class, which has two members: key and value. The only requirement to this class is that both key and value should implements the Comparable interface, otherwise Pair class will not accept them as type parameter. First…
cheng
  • 2,106
  • 6
  • 28
  • 36
4
votes
6 answers
4
votes
3 answers

Confusing explanation of limiting the type parameter of a generic binary search tree in Java

I'm new to generics (and Java, and Stack Overflow), and there is a point in the textbook I'm learning from where it discusses the implementation of a generic binary search tree (excerpt below). The Comparable interface is generic, so let’s consider…
ecl3ctic
  • 99
  • 4
4
votes
5 answers

Ordering enum values

I was wondering if there is any way of ordering enum for different classes. If for example, I have a fixed group of chemicals which react in different ways to other chemicals, some strongly, some weakly. I basically want to be able to switch up the…
Ester
  • 143
  • 1
  • 3
  • 12
3
votes
1 answer

Unchecked cast from generic T to comparable preventing compile

I am supposed to be implementing mergesort without recursion. I've finished all that jazz, but the class is not compiling for reasons outside the domain of the homework assignment. Here is the problem: This aspect is taken directly form the…
Ocasta Eshu
  • 841
  • 3
  • 11
  • 23