Questions tagged [compareto]

The CompareTo method is found in the .NET Framework and Java and imposes the natural ordering of a class. This is done by returning a negative integer, zero, or a positive integer as the instance of the implementing class is less than, equal to, or greater than the object it is compared to.

The CompareTo method is found in the .NET Framework and Java and imposes a natural ordering of a class. This is done by returning a negative integer, zero, or a positive integer as the instance of the implementing class is less than, equal to, or greater than the object it is compared to.

727 questions
197
votes
4 answers

BigDecimal equals() versus compareTo()

Consider the simple test class: import java.math.BigDecimal; /** * @author The Elite Gentleman * */ public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub …
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
195
votes
18 answers

How to simplify a null-safe compareTo() implementation?

I'm implementing compareTo() method for a simple class such as this (to be able to use Collections.sort() and other goodies offered by the Java platform): public class Metadata implements Comparable { private String name; private…
Jonik
  • 80,077
  • 70
  • 264
  • 372
131
votes
21 answers

compareTo() vs. equals()

When testing for equality of String's in Java I have always used equals() because to me this seems to be the most natural method for it. After all, its name already says what it is intended to do. However, a colleague of mine recently told me had…
Thomas Lötzer
  • 24,832
  • 16
  • 69
  • 55
104
votes
5 answers

Why is compareTo on an Enum final in Java?

An enum in Java implements the Comparable interface. It would have been nice to override Comparable's compareTo method, but here it's marked as final. The default natural order on Enum's compareTo is the listed order. Does anyone know why a Java…
neu242
  • 15,796
  • 20
  • 79
  • 114
70
votes
8 answers

compareTo with primitives -> Integer / int

Is it better to write int primitive1 = 3, primitive2 = 4; Integer a = new Integer(primitive1); Integer b = new Integer(primitive2); int compare = a.compareTo(b); or int primitive1 = 3, primitive2 = 4; int compare = (primitive1 > primitive2) ? 1 :…
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
40
votes
2 answers

Why are Byte.compare() and Integer.compare() implemented differently?

I am studying the source of the OpenJDK. My attention was attracted by the methods Byte.compare() and Integer.compare(): public static int Byte.compare(byte x, byte y) { return x-y; } public static int Integer.compare(int x, int y) { return…
Sergey Morozov
  • 4,528
  • 3
  • 25
  • 39
38
votes
4 answers

Comparison method violates its general contract! Java 7 only

I know this has been an issue for a while now, and checked all previously answers I could get, but still this one doesn't work. The object 'crew' represents crewmembers with ranks and other items. The comparison should be made by comparing…
user1007068
  • 381
  • 1
  • 3
  • 3
31
votes
1 answer

Why use TimeSpan.CompareTo() rather than < > or =

I've been going over some Microsoft samples of code for the Kinect sensor and have stumbled across the following line. TimeSpan zeroDuration = TimeSpan.FromSeconds(0.0); TimeSpan timeRemaining = ...; if (timeRemaining.CompareTo(this.zeroDuration) >…
Maxim Gershkovich
  • 45,951
  • 44
  • 147
  • 243
30
votes
4 answers

Override CompareTo: What to do with null case?

What should be returned in a CompareTo method when the given object is null? The MSDN Library shows a example where 1 is returned. But I would have expected to throw an error because comparing to null is not possible. I expect different opinions to…
John Threepwood
  • 15,593
  • 27
  • 93
  • 149
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
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
1 answer

Undocumented String.compareTo(null) NPE?

The following little test throws an NPE: public class Test { public static void main(String[] args) { String a = "a"; String b = null; System.out.println(a.compareTo(b)); } } Yet, the Javadoc of compareTo()…
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
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
21
votes
6 answers

What should int compareTo() return when the parameter string is null?

It is said that when input parameter is null, compareTo() should throw a NullPointerException. However, I am implementing a class which needs to compare fields with the type of String. These fields need not to be mandatory. I wonder in this case, 1)…
zw324
  • 26,764
  • 16
  • 85
  • 118
21
votes
5 answers

Scala idiom for ordering by multiple criteria

I want to do something like this: class Foo extends Ordered[Foo] { val x val y val z . . . . def compare(that: Foo) = { val c0 = this.length compareTo that.length // primary comparison lazy val c1 =…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
1
2 3
48 49