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
-4
votes
1 answer

Unconsistent natural ordering for TreeMap

Is it possible to create a TreeMap in which the natural ordering of the keys is not consistent(key1.equals(key2) = true, key1.compareTo(k3) = 1, key2.compareTo(key3) = -1)? If I will change the compareTo so that this won't happen but there will be…
Tomer Shinar
  • 415
  • 3
  • 13
-4
votes
1 answer

Java generic compareTo

I have developed/implemented a generic compareTo method in Java. So that for any object you define, it will hava a compareTo method. Is this relevant? Please answer and comment.
-4
votes
1 answer

Java Programming: How to use compareTo when comparing the names of the candidates in a text document

This method is used to compare the names of two candidates. Returns 0 if the name of this candidate is the same as the name of otherCan; returns < 0 if the name of this candidate is less then the name of otherCan; returns > 0 if the name of this…
E.A
  • 1
  • 2
-4
votes
1 answer

Sorting Arrraylist from an input file using Comparable/Compareto Methods

I am trying to create a program that reads an input file and outputs it three times. The last output showing all the values from the input and are sorted out using my compareTo method. I am trying to sort out the population on my array list but it…
-4
votes
6 answers

CompareTo() for finding the shortest/longest string in an array

I am trying to solve a exorcise that is supposed to learn me about the Comparable interface. It tells me to find the shortest and longest string in a string array. I think I am supposed to make my own compareTo() -method because the…
JonLunde
  • 151
  • 1
  • 3
  • 10
-4
votes
2 answers

Not understanding what is being passed into method

So I've been given pre-written code and I have to fill in the comepareTo function. It needs to compare strings and sort them by length but I'm not sure what is being passed in the method. import java.util.Arrays; public class testString…
-5
votes
1 answer

Comparing strings in Java?

I have few strings whom I have to compare and the print the smallest,For eg: A0< A1 A12< A22 A< B 12<23 a12< a21 Note:The string could be only number or only alphabet or can be mixture of both we have to compare it and print the smallest one…
Änsu Man
  • 3
  • 6
1 2 3
48
49