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

Making your own class 'Comparable'

I followed a tutorial, but failed to make my Country class Comparable for my BST. Main: BinarySearchTree A = new BinarySearchTree(); Country a = new Country("Romania", "Bucharest", 1112); A.insert(a); Country class: public int compareTo(Object…
Bogdan M.
  • 2,161
  • 6
  • 31
  • 53
7
votes
2 answers

What is the difference between comparable and any?

I have tried to use generics with Go, but I don't really understand when we use any or comparable as type parameter. Can someone help to understand these?
Teguh
  • 81
  • 4
7
votes
2 answers

What do < and > mean such as implements Comparable?

In Java 1.4.2, class java.math.BigInteger implements interfaces Comparable, Serializable. In Java 1.5.0, class java.math.BigInteger implements interfaces Serializable, Comparable. This is just an example to help me ask about < and >. …
eleven81
  • 6,301
  • 11
  • 37
  • 48
7
votes
2 answers

Java Sort with Comparable

I have an ArrayList of Person objects. A Person has name, age and height. My goal is to sort this ArrayList. I have implemented Comparable and have defined compareTo() but when I try to sort it, it give me this error: The method…
user6509972
  • 247
  • 1
  • 4
  • 9
7
votes
2 answers

Java Comparable: helper methods for isLessThan, isGreaterThan, isEqualTo

For objects a and b implementing the Comparable interface I want to avoid having code like if (a.compareTo(b) > 0) { ... } instead, I am looking for helper methods like if (a.isGreaterThan(b)) { ... } This would help me a lot for not…
DEX
  • 95
  • 1
  • 7
7
votes
2 answers

How to use HashSet to find common elements in two Comparable arrays?

EDIT: The method signature public Comparable[][] findCommonElements(Comparable[][] collections) is wrong. It should be public Comparable[] findCommonElements(Comparable[][] collections) but changing it in my IDE messes everything up. I…
DevOpsSauce
  • 1,319
  • 1
  • 20
  • 52
7
votes
2 answers

Extend @objc protocol with Comparable in Swift

I am trying to extend my protocol Option with Comparable to use simple .sort() method. Below short example only with Equatable to show errors. @objc protocol Option: Equatable { var title: String { get } var enabled: Bool { get } var…
dtd
  • 127
  • 1
  • 7
7
votes
1 answer

Sort list of objects using Collection.sort() with lambdas only

I am beginner in lambdas and trying to understand how it works. So I have this list of Student with id and score attributes and I have to sort it accoding to the score . My Code import java.util.*; class Student { int id, score; public…
singhakash
  • 7,891
  • 6
  • 31
  • 65
7
votes
2 answers

Generic class that conforms to Comparable in Swift

I'm attempting to create a simple generic node class that conforms to the Comparable protocol so that I can easily compare nodes without accessing their key. When I attempt to write the < and == functions, however, the compiler doesn't seem to like…
Daniel Rushton
  • 480
  • 5
  • 13
7
votes
3 answers

No interface word before interface Comparable

It is detail, but I want to know why this happens. Exemplary code: Class klasa = Enum.class; for(Type t : klasa.getGenericInterfaces()) System.out.println(t); Output o the program: java.lang.Comparable interface java.io.Serializable Why on…
Pawel
  • 1,457
  • 1
  • 11
  • 22
7
votes
3 answers

TreeMap put() silently deletes other entries?

I've experienced some really spooky TreeMap behavior and I've had some trouble narrowing down a small test case, so bear with me. I want to read a large number of key-value pairs into a Map, from a file provided at runtime. I'm using a custom key…
krivard
  • 652
  • 6
  • 17
7
votes
3 answers

Java: Sort a Collection using a CollatorKey

what I would like to achieve is to sort a colletion of objects by a string value. However in a locale dependant way using a collator. Due to performance reasons I do not want to use the Collator compare() method (as below in the code) rather the…
jan
  • 131
  • 2
  • 7
6
votes
3 answers

Ruby - Using Comparable mixin to compare objects on two different attributes

Is there an easy way (i.e. using the spaceship operator) to define comparison in Ruby based on two different attributes? I.e. If I have a class that contains two attributes, attr1 and attr2, is there a Rubyesque way of comparing two instances of…
Richard Stokes
  • 3,532
  • 7
  • 41
  • 57
6
votes
10 answers

Java Generics and Infinity (Comparable)

With the type Integer you can do this: int lowest = Integer.MIN_VALUE; What can I do if I use generics? K lowest = <...>; I need this in order to implement something similar to a PriorityQueue. I have access to a node I want to remove from the…
6
votes
2 answers

How does compareBy work in kotlin using a boolean expression

I know from official documentation that compareBy creates a comparator using the sequence of functions to calculate a result of comparison. The functions are called sequentially, receive the given values a and b and return Comparable objects. I know…
Saurabh Kumar
  • 437
  • 1
  • 5
  • 18