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

What determines ascending or descending order in Comparator / Comparable collection class?

I understand we can sort or order the objects, stored in Collection as per our requirement(s). While I get deep understanding, I am not convinced by the fact that ascending and descending order of arrangement is achieved by (a - b) ->ascending or (b…
David Prun
  • 8,203
  • 16
  • 60
  • 86
30
votes
5 answers

Min / max function of two Comparables

I need to find the smaller of two Comparable values: Comparable a = ...; Comparable b = ...; Comparable min = a.compareTo(b) <= 0 ? a : b; This is similar to Math.min(a, b), but for Comparable. I know that the ternary operator is already…
Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
29
votes
3 answers

How do I make 2 comparable methods in only one class?

I've got one class, that I sort it already by one attribute. Now I need to make another thing, that I need to create another way to sort my data. How can I make it, so I can choose between the two methods. The only command I know is Collections.sort…
Gondim
  • 3,038
  • 8
  • 44
  • 62
29
votes
9 answers

How do I write a compareTo method which compares objects?

I am learning about arrays, and basically I have an array that collects a last name, first name, and score. I need to write a compareTo method that will compare the last name and then the first name so the list could be sorted alphabetically…
Jeremy B
  • 863
  • 8
  • 18
  • 30
28
votes
4 answers

java.lang.Comparable and equals

If I implement java.lang.Comparable for a class, do I still have to override the equals() method? Or will the Comparable work for equals as well? If the answer is no, then what if some discrepancy arises? Let's say the way I term two objects as…
aps
  • 2,452
  • 10
  • 35
  • 47
28
votes
4 answers

Java Comparator using .reverseOrder() but with an inner class

I am creating a simple program to learn about the Java Comparator class. I have sorted an Arraylist into order but now I want to sort the list in descending order but am having problems in where to call the .reverseOrder() method as I have used an…
James Morrison
  • 283
  • 1
  • 3
  • 4
27
votes
4 answers

Implementing Comparable with a generic class

I want to define a class that implements the generic Comparable interface. While in my class I also defined a generic type element T. In order to implement the interface, I delegate the comparison to T. Here is my code: public class Item
frank.liu
  • 477
  • 1
  • 5
  • 11
26
votes
3 answers

Java "unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable"

I'm trying to implement a sorted list as a simple exercise in Java. To make it generic I have an add(Comparable obj) so I can use it with any class that implements the Comparable interface. But, when I use obj.compareTo(...) anywhere in the code I…
3mpty
  • 506
  • 1
  • 6
  • 14
26
votes
5 answers

How to use the Comparable CompareTo on Strings in Java

I can use it to sort by emp id but I'm not sure if it is possible to compare strings. I get an error the operator is undefined for strings. public int compareTo(Emp i) { if (this.getName() == ((Emp ) i).getName()) return…
Jack
  • 351
  • 2
  • 6
  • 10
25
votes
6 answers

Comparing Long values using Collections.sort(object)

I'm trying to sort a simple list of objects by a long - the below isn't working because one of the long strings is pushed to the top simply because it starts with a lower number. So I'm looking for a way to sort these by the actual long values…
Toran Billups
  • 27,111
  • 40
  • 155
  • 268
25
votes
5 answers

Why does compareTo return an integer

I recently saw a discussion in an SO chat but with no clear conclusions so I ended up asking there. Is this for historical reasons or consistency with other languages? When looking at the signatures of compareTo of various languages, it returns an…
user2336315
  • 15,697
  • 10
  • 46
  • 64
24
votes
2 answers

Java: to use contains in a ArrayList full of custom object should I override equals or implement Comparable/Comparator?

I have an ArrayList full of these: class TransitionState { Position positionA; Position positionB; int counter; public boolean equals (Object o){ if (o instanceof TransitionState){ TransitionState…
andandandand
  • 21,946
  • 60
  • 170
  • 271
24
votes
3 answers

Collections.sort() declaration: why rather than

Why does Collections.sort(List) have the signature : public static > void sort(List list) and not : public static > void sort(List list) I understand that they both would…
prvn
  • 684
  • 6
  • 21
24
votes
4 answers

Compare two objects with "<" or ">" operators in Java

How to make two objects in Java comparable using "<" or ">" e.g. MyObject obj1= new MyObject(“blablabla”, 25); MyObject obj2= new MyObject(“nannaanana”, 17); if (obj1 > obj2) do something. I've made MyObject…
IvanN
  • 307
  • 2
  • 4
  • 13
23
votes
6 answers

Why it is implied that objects are equal if compareTo() returns 0?

Let's have a class Person. Person has a name and height. Equals and hashCode() takes into account only name. Person is comparable (or we implement comparator for it, does not matter which one). Persons are compared by height. It seems reasonable…
Alpedar
  • 1,314
  • 1
  • 8
  • 12
1
2
3
83 84