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

how do I interpose on Long/String compareTo()?

I have a class implementing a data structure storing Comparable objects. Some instances hold Longs and other Strings. I want to count the number of comparisons that occur, without changing the data structure class or the application too much. One…
3
votes
1 answer

warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type Comparable

I'm recently returning to Java after 10 years and I'm pretty rusty. I'm trying to run some basic sorted list code I had. This code used to compile and run but now I'm getting the following warning: .\LinkedList.java:30: warning: [unchecked]…
shtone
  • 33
  • 3
3
votes
2 answers

compareTo Error: Cannot invoke compareTo(int) on the primitive type int

Not sure how to get the compareTo to not give error. Very lost. Tried changing multiples lines of code. public voide addValue(int newNumber){ int index = 0; while ((index < numbers.size()) && (numbers.get(index.compareTo(newNumber) ==-1)) …
Lil D
  • 31
  • 1
  • 1
  • 2
3
votes
5 answers

Comparing an Array of Comparables in Java

I want to compare an array of comparables. The simplest way seems the following (details not shown): public class ArrayComparable implements Comparable{ ArrayList> list = new…
delmet
  • 1,013
  • 2
  • 9
  • 23
3
votes
3 answers

Java compareTo Dates cannot be equal

I am trying to compare 2 dates, the first date is from the MySQL database, and the second is from the current date. as you can see below, there are different dates in the database But the problem is that I got 3 if statements, which should tell…
kristoffer
  • 65
  • 1
  • 7
3
votes
1 answer

Kotlin: generate a Comparable#compareTo() function from the properties of a class

In Kotlin, is there any concise way to generate a Comparable#compareTo() function for an interface that just calls this.property1.compareTo(that.property1) for each property of the interface in declaration order? Obviously, every property of the…
XDR
  • 4,070
  • 3
  • 30
  • 54
3
votes
4 answers

Should I be concerned about this compareTo/equals/hashCode implementation?

I'm in the middle of QA'ing a bunch of code and have found several instances where the developer has a DTO which implements Comparable. This DTO has 7 or 8 fields in it. The compareTo method has been implemented on just one field: private…
Chris Knight
  • 24,333
  • 24
  • 88
  • 134
3
votes
2 answers

Why is this a static binding instead of dynamic binding?

I'm still a little confused with regards to the difference between static and dynamic. From what I know dynamic uses object while static use type and that dynamic is resolved during runtime while static is during compile time. so shouldn't…
3
votes
4 answers

demonstrate that equals goes hand in hand with compareTo

It's written in all decent java courses, that if you implement the Comparable interface, you should (in most cases) also override the equals method to match its behavior. Unfortunately, in my current organization people try to convince me to do…
bvdb
  • 22,839
  • 10
  • 110
  • 123
3
votes
1 answer

How C# compareto method compares Strings

I want to know how CompareTo method of C# compares two strings, so I tested like this : string str1 = "0"; string str2 = "-"; Console.WriteLine(str1.CompareTo(str2)); // output : 1 string str3 = "01"; string str4 =…
huashui
  • 1,758
  • 2
  • 16
  • 24
3
votes
1 answer

Comparing generic fields

I have some generic types, like the following: public struct Tuple { ... } public struct Tuple { ... } etc. These should in theory be able to compare themselves against other values of the same type, so that I can write the…
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
3
votes
3 answers

Error while fetching data from sorted dictionary in C#

I have a SortedDictionary>. Following is the Package class: using System; namespace GetPackageInfo { public class Package : IComparable, IEquatable { public string PackageName; public string Version; …
3
votes
4 answers

How can I calculate age in Java accurately given Date of birth

I am trying to calculate age in java that accounts for months, so just subtracting years will not work. I also want to tell the user if today is their birthday. Here is the code I have so far, but I am afraid it is a bit off. It also will not tell…
alphamalle
  • 192
  • 1
  • 3
  • 15
3
votes
2 answers

Which usage of compareTo method is more understandable?

I want to sort objects based on Boolean values and I want to sort true values before false values. Which of these implementations of compareTo is more readable? Using -1 to change default behavior public class Example implements…
Joni
  • 3,327
  • 3
  • 25
  • 22
3
votes
3 answers

"double can not be dereferenced" [Java lang.]

I keep getting the error: "double can not be dereferenced" in the boolean line (line 45) I have never seen this error. inputdata.txt is a text file containing 8 Item class inputs. I would like to know what is wrong with my code and how I should go…
Spartan058
  • 31
  • 3