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

Why String implements Comparable and not Comparator interface

This question was asked in an interview. The question was why String and other wrapper classes implement Comparable instead of Comparator interface. I tried to explain that Comparator is basically to provide customized sorting and Comparable is for…
RoyalTiger
  • 511
  • 2
  • 8
  • 27
-3
votes
2 answers

How sort a string array based on the location of a word that the first string contains in a second string array, android, java

I am trying to sort a string array for use in a list view. I have two string arrays one with the words I want to display, the other with the sort order I want. Any words that aren't in the sort list could be at the end. For Example: String[] order =…
MJDude
  • 73
  • 10
-3
votes
2 answers

java compareTo substracts values

I am using a Binarytree and for my removeNode(E value) method (E extends Comparable), I use the method value.compareTo(root.getData()). Unfortunately it doesn't compare, but substracts both values and returns the result as an integer. Does…
Applecow
  • 796
  • 3
  • 13
  • 35
-3
votes
5 answers

Sorting algorithm that is stable in sorting ten million objects

I am trying to sort 10 Million Account objects in an array or array list. The Account class implements the comparable interface. with some variables such as age, acct number, etc. I need to sort this array or array list by age, and I need to keep…
-3
votes
2 answers

How do I make my list a sorted list?

I am working on an assignment for a programming course I am following and I am using a List to store data. The List class: public List() { init(); } protected Node first, current, last; public int numberOfNodes; public boolean isEmpty()…
-3
votes
3 answers

Correct it so that it will compile and properly implement Comparable

Why won't the following program compile? Correct it so that it will compile and properly implement Comparable. class Int implements Comparable { private int x; public Int(int x) { this.x = x; } public int compareTo(Int…
-3
votes
3 answers

Sorting Object of ArrayList with combination of different Attributes

My class is like this: class Employee{ int age; String name; int empId; . . . } In the main method i have created the object of Employee class and save it in ArrayList say objEmpList. Now,my requirement is to sort objEmpList like the…
Nirdesh
  • 63
  • 2
  • 11
-4
votes
3 answers

Compiler not Recognizing that I've implemented the compareTo() method from the Interface it Implements

I'm implementing an interface 'Comparable' by the abstract class AbstractAffiliate, which is extended by the abstract class Abstract Faculty, which is extended by the regular class Assistant. Upon implementing the compareTo method in the assistant…
Oliver
  • 31
  • 1
  • 4
-4
votes
2 answers

Without implementing the Comparable interface, how does the object of an ArrayList use Collection.sort() without an error?

Here ArrayList & its parent class & interface never implemented Comparable, Still the Collections.sort(al) compiled without an error ? public class Collections_Demo { public static void main(String[] args) { ArrayList al= new…
Amartya
  • 29
  • 3
-4
votes
2 answers

Implement a CompareTo in Java

I have an class PersonQ public class PersonQ { Queue queue; int Rows; public PersonQ(int r){ queue = new PriorityQueue(); Rows = r; } //Also I have getters and setters and a method to fill…
-4
votes
4 answers

compareTo() method in Java is not working

In java if s1 = "d"; s2 = "D" System.out.println(s1.compareTo(s2)); It will give output 32 always no matter what alphabet you use if they are same i.e. one is capital and one is smaller. Why is this so???
-4
votes
1 answer

Sorting Arrraylist from an input file using Comparable/Compareto Methods

I am trying to create a program that reads an input file and outputs it three times. The last output showing all the values from the input and are sorted out using my compareTo method. I am trying to sort out the population on my array list but it…
-4
votes
2 answers

Creating a Generic for a Class

Assuming you have a class named Rational where each object contains two ints representing the numerator and the denominator, write the class definition line if you wanted to indicate that it would implement the generic interface Comparable and write…
userb
  • 173
  • 2
  • 15
-4
votes
1 answer

sort list of employees in Java in which Boss name should name appeas first followed by other employees in a alphabetical order

I Want to sort a List of employee in Java in which Boss name should appear first followed by other employeees in a Alphabetical order. Employee employee1 = new Employee(); Employee employee2 = new Employee(); Employee…
-5
votes
2 answers

COMPARABLE - How to see if elements from objects are equals?

Im devoloping a program that i put some elements into my object within an ArrayList... this is my code import java.util.ArrayList; public class ListaConcepto { public static void main(String[] args) { // TODO Auto-generated method…
Antonio Alejos
  • 115
  • 1
  • 6
1 2 3
83
84