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

Sorting list of interface reference objects inherting ICompareable

I was having trouble with list.Sort() for a list of interface references that point to different types however the question Sort a list of interface objects provided the following solution solution interface IFoo : IComparable { int…
Jack Blackmore
  • 117
  • 1
  • 10
4
votes
1 answer

How does Comparator.comparing() function work?

For the code below: ForComparatorDemo object1 = new ForComparatorDemo("object-1",5); ForComparatorDemo object2 = new ForComparatorDemo("object-2",4); ForComparatorDemo object3 = new ForComparatorDemo("object-3",3); ForComparatorDemo object4 = new…
Milind Vinkar
  • 159
  • 1
  • 9
4
votes
4 answers

Why Java TreeMap not printing all its inserted entries

Pls tell what is wrong is happening here. I have a Person class which I'm using as a key in TreeMap. I have implemented Comparable also so that TreeMap can do sorting. public class Person implements Comparable{ private String name; private int…
user3626306
  • 137
  • 2
  • 2
  • 9
4
votes
2 answers

What is string lexicographically? Java

The compareTo() method in Java compares two strings "lexicographically". Can someone please simply explain how the lexicographic comparison works in java? I found this post that explains the three cases of <0 , ==0, and >0 ; However, I am still…
Jonathan Scialpi
  • 771
  • 2
  • 11
  • 32
4
votes
4 answers

TreeSet doesn't work, what's wrong with the following code?

The following code should print 3 persons, but it actually print 4 persons, why? Person("a", 1) and Person("a", 4) should be treated as the same, but they are not. import java.util.TreeSet; public class Person implements Comparable{ …
4
votes
3 answers

Java, how to use compareTo to sort an Arraylist

Im trying to figure out how to sort an ArrayList using comparable, my code looks like this: public class playerComparsion{ public static void main(String[] args){ ArrayList list = new ArrayList(); …
user3712130
  • 87
  • 1
  • 3
  • 8
4
votes
1 answer

Built-in compare on discriminated unions in f#

In answering this question, I discovered the following behaviour of compare on discriminated unions. type T = A | B | C | D compare A B (* val it : int = -1 *) compare A C (* val it : int = -2 *) compare A D (* val it : int = -3 *) I was…
Søren Debois
  • 5,598
  • 26
  • 48
4
votes
1 answer

Trouble understanding An excerpt from 'Effective Java' about compareTo

This is about the compareTo contract that classes can implement. there is no way to extend an instantiable class with a new value component while preserving the compareTo contract, unless you are willing to forgo the benefits of…
Anusha Pachunuri
  • 1,389
  • 4
  • 18
  • 39
4
votes
2 answers

Java generics, type variable scope

What's the difference between: public int compareTo(E e) // first line (compilation error) and public int compareTo(E e) // second line (OK) only in the second line i get through the compiler. Does it mean that it needs to be sure that that…
Rollerball
  • 12,618
  • 23
  • 92
  • 161
4
votes
3 answers

java Float: == equals compareTo

I have 2 questions. 1) I was told that when comparing two Float or Double data, use compareTo instead of equals. I don't know the reason. Is there any example that shows where using equals will lead something wrong? 2) See this code: float…
user1703055
  • 149
  • 2
  • 9
4
votes
5 answers

Is it possible to override String's compareTo method in Java?

I have a class that generates random IP addresses. I have to sort this list but I need to use my own logic to compare the two strings. The way I would prefer to do it is to override the compareTo method in the String class and use Arrays.sort()…
jacky
  • 195
  • 6
  • 17
4
votes
7 answers

What is a practical application of Java's compareTo method?

In the Java textbook I'm learning from, it says that this uses "lexicographic ordering" to return an integer. I understand how it works, but what is a specific way this is used in programming?
Jared
3
votes
7 answers

Java compareTo for String and Integer arguments

I am implementing the bubble sort algorithm and I want it to be able to accept both Integer and String parameters. I cast all input as Strings and use the compareTo method to compare the integers casted as strings to the strings. I am getting an…
Ben
3
votes
3 answers

How does CompareTo sort a list?

Below, a list l that contains a list of Product with Name and Price properties. The list can be sort alphabetically by the following class ProductNameComparer which implements IComparar. List l = p.GetList(); l.Sort(new…
KMC
  • 19,548
  • 58
  • 164
  • 253
3
votes
3 answers

using compareTo in Binary Search Tree program

I've been working on this program for a few days now and I've implemented a few of the primary methods in my BinarySearchTree class such as insert and delete. Insert seemed to be working fine, but once I try to delete I kept getting errors. So after…
Scott Rogener
  • 31
  • 1
  • 2