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

How do I perform an encoding-independent string-comparison in Java?

I'm having a strange problem comparing strings. I send a string to my server (as bytes using getBytes()) from the client. I've ensured that encoding is the same on the client and server by starting both of them with -Dfile.encoding=UTF-8. I noticed…
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
5
votes
1 answer

How does the sort() method of the Collection class call the Comparable's compareTo()?

Suppose I want to sort a list of Employee objects: Employee emp1 = new Employee("Abhijit", 10); Employee emp2 = new Employee("Aniket", 5); Employee emp3 = new Employee("Chirag", 15); List employees = new…
5
votes
1 answer

Using PowerShell to find the differences in strings

So I'm playing around with Compare-Object, and it works fine for comparing files. But what about just strings? Is there a way to find the difference between strings? CompareTo() is good about reporting that there is a difference, but not what the…
James Brown
  • 327
  • 2
  • 11
  • 21
5
votes
2 answers

ByteBuffer - compareTo method might diverge

Based on the article here, compareTo method on the ByteBuffers might not work correctly when dealing with negative numbers bytes in Java are signed, contrary to what one typically expects. What is easy to miss though, is the fact that this affects…
Bober02
  • 15,034
  • 31
  • 92
  • 178
5
votes
1 answer

treemap uses compareTo instead of equals for containsKey()

I try to use the containsKey method of a TreeMap, but somehow I have problems with it. The objects stored in the treemap are defined such as equals() does not deliver the same result as compareTo(). this is intended. However, the doc of…
user3320142
  • 85
  • 1
  • 6
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
5 answers

Java - modified compareTo method says it needs to return an int, but it should be returning one

I'm learning basic Java right now and have a problem with my code that I can't figure out. It's basically what the title says. My Java compiler is telling me that there's an error with my custom compareTo method, saying that it needs to return an…
5
votes
1 answer

Java no autoboxing for int for compareTo method?

class Test{ public static void main(String[] args){ int a = 1; int b = 5; Integer c = new Integer(1); Integer d = 5; //autoboxing at work System.out.println(c.compareTo(d)); …
Kailua Bum
  • 1,368
  • 7
  • 25
  • 40
5
votes
1 answer

Rules to implement compare method

like compareTo, that have to be "reflexive, antisymmetric and transitive", are there any rules to implement the compare method?? thanks
Peppe
  • 315
  • 5
  • 13
4
votes
4 answers

compareTo() method is not overriding default method when using Comparable interface

I am trying to overwrite the default compareTo() method in java by writing my own and using implements comparable, however it seems that java is still using the default method. I am trying to sort an array of Strings by length that I get from a .dat…
Special K
  • 41
  • 1
  • 1
  • 2
4
votes
1 answer

Problem with CompareTo

I am trying to implement a sorted list. I have created the class I want to have stored in the list, but for some reason when I try and run the sort the sort method I get an exception thrown. It appears something behind the scenes is passing in a…
user589195
  • 4,180
  • 13
  • 53
  • 81
4
votes
1 answer

What is the difference between these two compareTo methods?

I want to understand what is the difference between these two methods? Can they both be used for the same problem? Or are they designed for different cases? public int compareTo(Coordinates o) { if (row < o.row) return -1; if (row > o.row)…
thpthp
  • 93
  • 1
  • 3
4
votes
2 answers

Objects added to a PriorityQueue are not ordered by their priority

I'm trying to implement a heap using a PriorityQueue as follows: PriorityQueue heap = new PriorityQueue(); Set allWords = codebook.getAllWords(); for(String word : allWords) { heap.add(new Node(word,…
kshen
  • 525
  • 2
  • 9
  • 17
4
votes
3 answers

Does compareTo have some sort of pre-launching delay?

I just found this statement: "One can greatly increase the performance of compareTo by comparing first on items which are most likely to differ". Is it true? And if it is, why?
dhblah
  • 9,751
  • 12
  • 56
  • 92