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

java exception: Comparison method violates its general contract

I have already checked previous posts on this topic -- this and this. In spite of that, I couldn't figure out how the contract violation can happen in my code given below. public class ScoreComparator implements Comparator>…
Raghava
  • 947
  • 4
  • 15
  • 29
3
votes
3 answers

compareTo Method with Doubles[Double Cannot Be Dereferenced]

Today I am working on a new program to find the cheapest pizza in a file full of data. i am completely stuck on one issue though, it is using the compareTo method. I created it in my resource class but I am not getting errors saying a double cannot…
user3337381
3
votes
2 answers

How to have a TreeSet which is "inconsistent with equals"

I have read numerous posts about TreeSets, Comparable/Comparator Interfaces, equals, compareTo, compare methods and I know that API says you have to make your ordering "consistent with equals" or weird things might happen. But in my case and I think…
akifusenet
  • 192
  • 6
3
votes
3 answers

Overriding compareTo method in order to sort objects by String values

I have a class that implements the Comparable interface. In this class I need to override compareTo method in order to sort objects by String values. If you scroll down to the bottom I'm attempting to make my method & I need to in the main method…
Adam Staples
  • 396
  • 2
  • 7
  • 19
3
votes
6 answers

Java compareTo() method returns classCastException

Let's assume I have an Employee base class and Manager subclass which extends Employee.Now let's say I create an object x of type Employee and object y of type Manager and call x.compareTo(y) no exception is triggered and x and y is compared as…
user1613360
  • 1,280
  • 3
  • 16
  • 42
3
votes
1 answer

Java Comparable Interface compareTo method

I don't see anything that I am doing wrong, but NetBeans gives me the following error: incomparable types required: boolean found: java.lang.Object public int compareTo(Object obj) { if( obj instaceof Employee){ Employee employee =…
user69514
  • 26,935
  • 59
  • 154
  • 188
3
votes
2 answers

Integer value comparison with null vallue

Hi I had a question related to Integer comparison. say I have two Integer count1 and count2, I want to implement the following action: if (count1 bigger than count2) do something; else do something else; I know I can use…
Vortex
  • 147
  • 1
  • 2
  • 9
3
votes
2 answers

Extending comparable interface and override compareTo

I know this has propably been asked 1000 times, and i have seen 1000 answers, but i didn't see any that actually worked for me. I have to sort a List of objects which implement my own interface. I decided to extend Comparable interface and implement…
Alexej Geldt
  • 33
  • 1
  • 1
  • 4
3
votes
3 answers

implementing comprable method in a generic way in Java for sorting

I am trying to implement a generic selection sort which can take any objects and do the sorting on it. I can promise to the compiler that whatever objects I am comparing, has the compareTo method implemented for it. But I get compile error for the…
brain storm
  • 30,124
  • 69
  • 225
  • 393
3
votes
3 answers

How to make the compareTo method respect the general contract?

Chromosome contains a number of scores generated in different ways. The compareTo method actually tests the agreement of the methods and accordingly returns a result. return 1: comp = -5..-1 return 0: comp = 0 (can happen in different scenarios…
jallmer
  • 589
  • 3
  • 17
3
votes
5 answers

Minimum string as per String#CompareTo

Ok, I'm sure that this must exist on here somewhere, but I can't seem to find it. Is there, and if there is what is, a minimum (non-null) String sequence according to String#CompareTo? I'm guessing "" but I'm not entirely sure.
Jeff
  • 12,555
  • 5
  • 33
  • 60
3
votes
5 answers

Internal working of the Sort() and CompareTo() methods

I've been trying to figure out how the CompareTo() method works internally and I failed. I've searched this site and read some posts, and I think I've seen all there is to see in MSDN about this subject and I just don't seem to get it. An MSDN…
JaviML
  • 41
  • 1
  • 6
3
votes
2 answers

How to sort a linked list with objects in java

I've created a linked list (generic containers) with objects in Java. I need to re-write my insert-method to make the list sorted alphabetically by keys. This is my code so far: Container: class Sellbeholder, V> implements…
Jectson
  • 79
  • 2
  • 9
3
votes
2 answers

Version object is comparing two versions incorrectly

I am using the Version object in order to run a CompareTo against two version numbers and making X happen if the version is >= versionA. My issue is that in the below code, the CompareTo is declaring that VersionB is greater than VersionA. Version…
Chris Bohatka
  • 363
  • 1
  • 4
  • 14
3
votes
2 answers

How does the compareTo() method, compare strings?

Such as if I were to compare the Strings "Hello" and "World". How does it know Hello is greater than World? The only thing I can come up with is, maybe it uses the ASCII Table as reference? Thanks for the help!
ekeitho
  • 109
  • 1
  • 2
  • 8