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

Using comparable to compare different variables

I'm familiar with standard comparisons using the Comparable interface, although today I'm having some trouble when I want to compare several different variables. I basically want to implement a compareTo method that yields the result -1 only when…
Deminth
  • 75
  • 7
5
votes
4 answers

Direct comparator in Java out of the box

I have a method which needs a Comparator for one of its parameters. I would like to pass a Comparator which does a normal comparison and a reverse comparator which does in reverse. java.util.Collections provides a reverseOrder() this is good for the…
KARASZI István
  • 30,900
  • 8
  • 101
  • 128
5
votes
3 answers

Why does the Java Collections Framework offer two different ways to sort?

If I have a list of elements I would like to sort, Java offers two ways to go about this. For example, lets say I have a list of Movie objects and I’d like to sort them by title. One way I could do this is by calling the one-argument version of…
dvanaria
  • 6,593
  • 22
  • 62
  • 82
5
votes
1 answer

What is the convention with Java beans, and implementing interfaces like Comparable?

Java Beans, as far as I know, should always: Have only an empty constructor Have only fields, and getter/setter methods for these fields. However, I am wondering what the convention is for Java beans to implement interfaces like Comparable? I…
jumps4fun
  • 3,994
  • 10
  • 50
  • 96
5
votes
2 answers

Java - how to sort object in many ways: Arrays.sort(), Comparable

Let's say that I have an array with objects, where I have some employees (objects). They all have: int age, double salary. I want to sort this array so my class implements Comparable . I've made a method: public int compareTo(Employee…
RichardK
  • 3,228
  • 5
  • 32
  • 52
5
votes
3 answers

How to define hash function for a class including comparable?

Imagine a class including comparable like this: class Element include Comparable attr_accessor :name, :pos_x, :pos_y def initialize(name, pos_x, pos_y) @name = name @pos_x = pos_x @pos_y = pos_y end def <=>(other) if…
Paddi91
  • 93
  • 5
5
votes
2 answers

Overriding a compareTo from an extended class - what's going on?

I've been trying to override a compareTo in such a way: This is the original: @Override public int compareTo(ProductPart6 s) { return this.getproductName().compareTo(s.getproductName()); } this is what i'm trying to override it with: it…
user2391210
  • 53
  • 1
  • 5
5
votes
6 answers

Java override compareTo, Long

I have a class that implements the Comparable interface. In this class I need to override compareTo method in order to sort objects by Long values. What I don't know is how to perform is the comparison of the Long type. I get error when trying to…
user1121487
  • 2,662
  • 8
  • 42
  • 63
5
votes
4 answers

Comparator for sorting an object arraylist by float parameter

How can i create a comparator to be able to sort an arraylist by a float parameter? Imagine i have an arraylist of objects that has something like this: ID=7 TFIDF=0.12654299 PR=25238.0 ID=4 TFIDF=0.12654299 PR=3638.0 ID=4 TFIDF=0.12654299…
Pedro Neves
  • 364
  • 1
  • 8
  • 26
5
votes
1 answer

Java - Collection.Sort over Interface Objects

I have a number of abstract classes each superclassing three or four concrete ones and of the form: public abstract class TypeOfMapObject extends IrrelevantClass implements Serializable, MapObject, Comparable { //irrelevant stuff …
MrB
  • 818
  • 8
  • 28
5
votes
7 answers

Sorting ArrayList of Arraylist in java

I have an ArrayList of ArrayList of String. In Outer ArrayList on each index each Inner ArrayList has four items have four parameters. Contacts Id Contacts Name Contacts Adress Contacts Number Now I want to sort the complete ArrayList of the on…
Nikhil Agrawal
  • 26,128
  • 21
  • 90
  • 126
5
votes
3 answers

How to create SortedSet (e.g. TreeSet) for elements of type BitSet

I have a number (power(2,k)) of BitSet objects and I want to store them in a SortedSet. I use the code: Set S= new TreeSet<>(); However, I am getting this error: java.lang.ClassCastException: java.util.BitSet cannot be cast to…
Kaur
  • 279
  • 1
  • 6
  • 18
5
votes
3 answers

Sort my ojects which are parsed from json data

My Android client get server JSON response as follows: {"students":[{"id":1,"name":"John","age":12}, {"id":2,"name":"Thmas","age":13} {"id":3,"name":"Merit","age":10} ...]} My Android client code parses the…
john123
  • 589
  • 3
  • 6
  • 16
5
votes
4 answers

How to sort LinkedList?

I need to sort the Strings of a LinkedList by the length of the Strings, but would like to keep the order of same-length strings (not sorted lexicographically). Sample input: this is just a test Sample Output: a is this just test I am trying to do…
user1706571
  • 63
  • 1
  • 4
5
votes
2 answers

Why does Set.contains() not seem to be using o.equals()?

I have a TreeSet containing wrappers which store a Foo object at a certain position, defined like so: class Wrapper implements Comparable { private final Foo foo; private final Double position; ... @Override boolean equals(Object…
bountiful
  • 814
  • 1
  • 8
  • 22