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 generics issue with compareTo and inheritance

so I have a problem with my generics code. I've created class Person and Student extends Person. When I was trying to compare both students it suddenly perceived it as a Person and group number wasn't even included. Here is my Person class public…
Szadury
  • 3
  • 2
-3
votes
2 answers

Combining two arrays of strings into alphabetical order

I have two arrays of strings that I am trying to combine into one array. The two arrays are in alphabetical order. I have to combine them into alphabetical order. The way I attempted to do this was to create the combined list as the first list…
-3
votes
1 answer

java compare method explanation

public int compare(Map.Entry o1, Map.Entry o2){ if(o1.getValue() < o2.getValue() ){ return -1; } else if(o1.getValue() > o2.getValue()){ …
-3
votes
1 answer

How to compare two dates without the use of objects?

/** Reports whether or not date1 comes earlier in time than date2. For example, isEarlierThan("12-01-2015", "02-15-2017") is true but isEarlierThan("10-11-2016", "10-11-2016") and isEarlierThan("09-09-1967", "02-15-1933")is false. * / public…
-3
votes
3 answers

1. java ')' expected error + 2. 'else' without 'if' error

The error ')' expected is encountered on line 8; and the error 'else' without 'if' is found on line 9. This is only a part of the method. The beginning declares all needed variable (name1, name2, count), and states possible exceptions when reading…
Carrie
  • 11
  • 1
  • 5
-3
votes
1 answer

How does this.method variable differs from object.method when comparing two objects with Comparable interface?

Taking for example these code snippets: public int compareTo(DSA_Student C){ if (this.getName().compareTo(C.getName())<0) return -1; else if (this.getName().compareTo(C.getName())>0) return 1; else return (new…
Tia
  • 1,220
  • 5
  • 22
  • 47
-3
votes
2 answers

java compareTo substracts values

I am using a Binarytree and for my removeNode(E value) method (E extends Comparable), I use the method value.compareTo(root.getData()). Unfortunately it doesn't compare, but substracts both values and returns the result as an integer. Does…
Applecow
  • 796
  • 3
  • 13
  • 35
-3
votes
1 answer

What method can compare three integers? compareTo?

Can compareTo compare 3 integers? Reason I am asking is because I need to have a method that compares the month, day, and year to see if they are equal.
JetSeeker
  • 1
  • 1
-3
votes
1 answer

Java PriorityQueue and NullPointerException

I'm not sure why I'm getting a null pointer when I try to add the Node initialState ( which implements compareTo method). the following is my Node class. package puzzle; public class Node implements Comparable { int[][] state; Node…
-3
votes
1 answer

java compareTo Comparison method violates its general contract

I'm attempting to sort a collection and getting the following exception: java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:835) at…
Paul Creasey
  • 28,321
  • 10
  • 54
  • 90
-3
votes
1 answer

How do I group three object arrays under one object?

Apologies for the wording, I've been at this all day and I 1/2 don't know what I'm doing. Line 44 of the source code in the link, I want to make an object array that contains references to object arrays rectangle/words/dates. When I try to reference…
mmm_robots
  • 43
  • 1
  • 5
-3
votes
2 answers

What does the interface Comparable compare in Java?

I'd like to know what the interface Comparable compares in Java. Let's say I have two Nodes with only one instance variable each. How come node1.compareTo(node2) works? Thanks!
Amir David
  • 33
  • 2
-3
votes
1 answer

How does the comapareTo method in Java works?

Can anyone explain how does the following codes works? public int compareTo(Object o) { if (o instanceof Item) { return date1.compareTo( ((Item) o).getDate2()); } return -1; }
user1503699
  • 55
  • 1
  • 3
  • 8
-4
votes
2 answers

compareTo() method for a string giving out and bad operand type error

i'm doing a compareTo() method for a string the way i was taught but it's giving me an error, it seems that the way i formulated it similar to how you would usually do for a number doesn't go well with a string. Class Car(implements the interface:…
HoLee
  • 1
  • 4
-4
votes
1 answer

Return of CompareTo in C#

I understand the "CompareTo compares the instance and the value(represented in my code by amountA - instance and amountB-value) and it could return 1, -1 or 0 depending on what we have for the instance and value. Could anyone explain me why it…
jsbrcad
  • 33
  • 1
  • 7
1 2 3
48
49