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

Does the specific signed integer matter when implementing compareTo in a Comparable class?

When implementing compareTo(), does the degree of "difference" need to be taken into account? For instance, if I have 3 objects, C1, C2, and C3, such that C1 < C2 < C3. Should C1.compareTo(C2) return an integer that is less than…
javanix
  • 1,270
  • 3
  • 24
  • 40
9
votes
5 answers

Comparing two strings in java character by character

I am beginner in java, I am trying to compare two strings in java char by char and find how many different chars they have by the following code but it doesn't work, min is the min between the 2 strings for(int i=0; i
mike
  • 147
  • 1
  • 3
  • 13
8
votes
2 answers

cannot be cast to [Ljava.lang.Comparable

So I need to do dynamic ordered list. public class DynArrayListOrd> { private T[] tab ; public DynArrayListOrd() { tab = (T[])new Object[startSize]; } .... main…
szufi
  • 219
  • 1
  • 2
  • 9
8
votes
4 answers

Implementing custom compareTo

@Override public int compareTo(Object t) { if(t instanceof Student) { Student s = (Student)t; return (this.name.compareTo(s.name)); } else return -1; } This is my compareTo method implementation for…
Anjan Baradwaj
  • 1,219
  • 5
  • 27
  • 54
7
votes
3 answers

compareTo and equals in PriorityQueues

i'm a little confused with all the "If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave strangely." warnings in the Javadoc. I'm not even sure anymore a PriorityQueue is what i need... My…
foobar
  • 73
  • 1
  • 4
7
votes
3 answers

Effects of violating compareTo transitivity contract due to numerical precision errors

I have some numbers I'm trying to compare. They represent lengths of paths through different spaces. Unfortunately for me, some imprecision was causing false comparisons. For instance, after noting the wrong effects, I found that I was having…
code11
  • 1,986
  • 5
  • 29
  • 37
6
votes
3 answers

abstract compareTo method not being overrided

When compiling the code below, I get the following error: PersonalInformation is not abstract and does not override abstract method compareTo(Object) in Comparable I assume that means I have a problem with my compareTo method. But everything…
bitva
  • 195
  • 3
  • 4
  • 20
6
votes
5 answers

Java compareTo method beginner level

I have an assignment where I need to create a Student class where you store the student's neptun code (String nep_c) and the number of points you have achieved in the exam (int point_num). Prepare a public int getMark () method that returns the…
Kicsi7
  • 63
  • 4
6
votes
2 answers

Simplest way to get the lesser of two BigDecimal values in Java

Is there some way to determine which of two BigDecimal objects is the lower (smaller) number that is simpler than an if or a ternary operator calling BigDecimal::compareTo? Given: BigDecimal x = … ; BigDecimal y = … ; Either: if( x.compareTo( y ) <…
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
6
votes
2 answers

VB6 Object Comparison

What VB6 method allows two custom objects of the same type (defined in a class module) to be compared to each other? I think there's an equivalent to Java's compareTo method, but I can't find it anywhere.
derekerdmann
  • 17,696
  • 11
  • 76
  • 110
6
votes
4 answers

Sorting a List alphabetically using compareTo() method

I am writing a phonebook program in java and i need to list people in the list alphabetically and to do that i need to write a sorting algorithm for a list in java and it should use only compareTo() method. So can anyone help me to do that? public…
Master Noxob
  • 63
  • 1
  • 1
  • 8
5
votes
3 answers

Object.CompareTo(Object) for unknown data type

I am trying to compare objects in an object[] that are of a single type (unknown at runtime). They are of System.string, int, decimal, Datetime, or bool types. Is there a way to compare two of these objects to determine if one is greater or less…
ChandlerPelhams
  • 1,648
  • 4
  • 38
  • 56
5
votes
4 answers

Is there a library to compare primitive type values?

I am implementing Comparable interface on a trivial class that wraps a single int member. I can implement it this way: @Override public int compareTo ( final MyType o ) { return Integer.valueOf( this.intVal…
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
5
votes
2 answers

Why is CompareTo on short implemented this way?

Consider the following code: namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine(100.CompareTo(200)); // prints -1 …
sloth
  • 99,095
  • 21
  • 171
  • 219
5
votes
4 answers

How to compare two datetimes

I wonder how to compare two DateTime objects in .NET using DateTime methods Compare, CompareTo or Equals without comparing ticks. I only need a tolerance level of milliseconds or seconds. How can this be done?
Jonas
  • 3,155
  • 5
  • 35
  • 55
1 2
3
48 49