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

implementing Comparable interface from abstract class with concrete subclasses

package geometricobject; public abstract class GeometricObject implements Comparable { private String color = "white"; private boolean filled; private java.util.Date dateCreated; protected GeometricObject(){ dateCreated = new…
MISMajorDeveloperAnyways
  • 1,359
  • 3
  • 14
  • 20
3
votes
1 answer

How can multiple static methods use the same generic type?

While learning from Algorithms (4th edition) (by Robert Sedgewick; Kevin Wayne), I am trying to recreate their codes. They have raw uses of parametrized constructors (generics) which I assume might be because 2011 Java code is different from today's…
Aman
  • 43
  • 5
3
votes
1 answer

Unable to implement Comparable interface on Kotlin Enum

I would like my Enum values to be comparable with other types, for instance, String. I am not entirely sure why it complains and what the error means. enum class Fruits(val value: String): Comparable{ Apple("apple"), …
Arturs Vancans
  • 4,531
  • 14
  • 47
  • 76
3
votes
1 answer

Differences in an exported, signed apk and version running in Eclipse debugger? Serializeable class causing problems

Hopefully this won't be too long-winded, but trying to be complete: So I have an app on the Android Market. Within the app are a few Serializable classes. App is working fine everywhere (in emulator, on debugging phone, on phones that download the…
Joey
  • 464
  • 2
  • 9
3
votes
3 answers

how to compare on multiple classes in java?

Right now, I have written at comparator that sorts an array of Integers and Strings. As you can see from the code, if the two classes aren't the same, then the String class takes the greater than value. However, this only allows for two classes.…
emdog4
  • 1,975
  • 3
  • 20
  • 25
3
votes
1 answer

Java Comparable and TreeSet

Why do objects put in a TreeSet have to implement the Comparable interface? I'm trying to create a TreeSet containing some objects of my own class but ClassCastException is thrown when I try to add objects in the set.
asim
  • 73
  • 5
3
votes
1 answer

warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type Comparable

I'm recently returning to Java after 10 years and I'm pretty rusty. I'm trying to run some basic sorted list code I had. This code used to compile and run but now I'm getting the following warning: .\LinkedList.java:30: warning: [unchecked]…
shtone
  • 33
  • 3
3
votes
2 answers

How is the rule for sorting a list in ascending order and descending order?

I have been trying to understand how sorting using Comparable works. I have class Activity implementing Comparable interface. I am trying to understand how activity.finish-this.finish; sorts in descending order and how this.finish-activity.finish…
Nichole
  • 189
  • 2
  • 7
3
votes
2 answers

Swift comparable on a protocol

Happy new year everyone protocol Birthday { var date: Date { get set } } class Person: Birthday { var date: Date var fullName: String // ... } class Car: Birthday { var date: Date var color: UIColor // ... } I…
אורי orihpt
  • 2,358
  • 2
  • 16
  • 41
3
votes
2 answers

ClassCastException when overriding a super's method (Comparable)

I did search but couldn't find a similar problem. I'm sorry if this is duplicated. I write a regular queue method and try to extends it to have a priority queue. I don't understand why I can only insert if I use the super class' method, and not the…
Louis Tran
  • 1,154
  • 1
  • 26
  • 46
3
votes
4 answers

Implementing the comparable interface

I just found this exam question and cannot figure it out : The following depicts a contrived partial class which implements the Comparable interface. The only purpose of this contrived class is comparing its instances with a given string. There are…
John Curtsy
  • 541
  • 3
  • 8
  • 15
3
votes
2 answers

Comparator from nested POJO

I want to sort a list of my POJO by a property that is also a POJO. The Comparator should move null values to the end of the list. The nulls are already at second POJO level. Assume following POJOs: public class Foo { private Bar bar; …
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
3
votes
1 answer

Comparable Generic how to use ist

I am very new in Java. I dont get the idea of this >. I mean why a T before extends? Is that not enough to write extends Comparable? Why extends and not implements in javadoc its an interface, right? As I understand…
Sayuri
  • 63
  • 6
3
votes
2 answers

How to create a comparator with dynamic rules?

I need to sort a List of items based on a List of Filters, which are ordered by priority. However, these Filters come from the API request body, so they can change. I have a Filter class public class Filter { private String fieldName; private…
3
votes
1 answer

CharSequence interface in Java 11 added method `compare`. Why not `compareTo` of Comparable interface?

The CharSequence interface gained a new static method in Java 11: compare. This method returns an int: the value 0 if the two CharSequence are equal; a negative integer if the first CharSequence is lexicographically less than the second; or a…
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154