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
1 answer

Can anyone suggest how to implement the sorting by price of a product in android Recycler View

As I needed to sort the values of an array List, I'm unable to get the sorting algorithms for the different algorithms like Selection sort, quick sort and bubble sort. I'm new to Android app development, please help me to solve this. thanks in…
Manjunath
  • 52
  • 1
  • 10
-6
votes
1 answer

What is the internal sorting technique used in comparator and comparable interfaces and why?

With regards to both Comparable and Comparator interface in Java, i wanted to ask, what is the sorting technique used internally, and any reason for using the sorting technique in comparison to other sort techniques?
Doomed93
  • 418
  • 4
  • 10
-6
votes
1 answer

Bad Operand error in a Movie Session Program

I get the error: "Bad Operand Types for Binary Operator ">" first type: Time second type: Time" Code: public class MovieSession implements Comparable { private String movieName; private char rating; private Time…
WonderGal
  • 49
  • 1
  • 9
-6
votes
1 answer

What is the use of Cloning in JAVA and where it is frequently used?

I have used programming and understood that cloning means the duplication of a object. But, I could not get idea about in what context and where it is mainly used? I mean, where does it appear to be used? public abstract class GeometricObject { …
-7
votes
1 answer

Collections.sort(List) not applicable for HashSet

I have created a HashSet which contains elements of type Book(name, author). I have implemented the Comparable interface. But calling Collections.sort() results an error. The method sort(List) in the type Collections is not applicable for the…
shruti kharge
  • 115
  • 1
  • 2
  • 8
-7
votes
2 answers

Using Comparable interface to sort JAVA

Hi so for example i have : string1[0] ="Banana"; string1[1] ="Apple"; string1[2] ="Pineapple"; string1[3] ="Mandarin"; I want to sort this alphabetically using comparable and compareTo() method. So the result would…
Tommy
  • 427
  • 2
  • 5
  • 14
1 2 3
83
84