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

java comparable interface in sorting

I came up with the problem in sets is to build my own default sorting order using comparable interface. I want to arrange (int eid,String ename) in descending order based on eid only. So this was the logic I couldnt understand in the comapreTo…
Digs
  • 193
  • 1
  • 11
2
votes
1 answer

How to use compareTo with a node?

I have this simple node: public class Node implements Comparable{ T value; Node next; public Node(T value){ this.value = value; this.next = null; } public int compareTo(Node other){ return…
user5156323
2
votes
2 answers

Comparing two dates using Comparable interface

I am currently designing a GUI for a bank database application. In my GUI I have a "List accounts opened before" button that I am trying to code to list all of the accounts in the database in a text area that have dates before a date that the user…
loggingco
  • 23
  • 1
  • 5
2
votes
1 answer

How to use compareTo to compare strings to get names in alphabetical order instead of scores?

I have to use the given codes and alter it so that instead of returning results by comparing scores, it should return the data input by the names in alphabetical order. Here is the ZIP FILE Here are my results when I run the GolfApp2 file: …
pyuntae
  • 742
  • 3
  • 10
  • 25
2
votes
2 answers

Java's compareTo() function equivalent in Perl?

What's the Perl function that achieves the same thing as compareTo() in Java? I know about eq and ne but I want to compare to see if one string is greater than another.
vette982
  • 4,782
  • 8
  • 34
  • 41
2
votes
1 answer

Overriding compareTo(T t)

I did a class "People" which has a String name. Now I want to compare two objects using TreeSet. public class People implements Comparable { public TreeSet treeSet; public String name; public People(String name) { …
szufi
  • 219
  • 1
  • 2
  • 9
2
votes
1 answer

How to sort array of keys based on values stored in separate array

How to sort an array of keys based on values stored in a separate array in C# Example int[] keys = new int[] {1, 2, 3, 4, 7}; double[] vals = new double[] {0.5, 0.2, 0.3, 0.1, 0.4}; I would like to sort keys array based on values in vals array ie.…
Sebastian Widz
  • 1,962
  • 4
  • 26
  • 45
2
votes
2 answers

Override compareTo() method in custom Java class?

I have both a Dog and Cat class which look something like this: public class Dog implements Speakable { private String name; public Dog(String name) { this.name = name; } public void speak() { System.out.println("Woof! Woof!"); } public…
JoeSchmuck
  • 101
  • 3
  • 8
2
votes
5 answers

Sorting Object ArrayList using Collections.sort

I have added some Integers to an ArrayList of object type, and want it to be sorted. My code looks like: List list = new ArrayList(); list.add(24); list.add(2); list.add(4); Collections.sort(list); // getting error…
ak pk
  • 21
  • 3
2
votes
5 answers

Javadoc comments - CompareTo?

My book asks me to write a Javadoc comment for a section of code. For the most part I understand how to do javadocs, but I do not understand what the program is doing. "Write a Javadoc comment for the following method of a class Person. Assume that…
2
votes
1 answer

Sorting an ArrayList of subclasses using Comparable and compareTo()

I have a bank account program that implements a BankAccount superclass which extends to CheckingAccount and SavingsAccount subclasses. Each account has four properties: first name, last name, social security, and balance. I have a BankDatabase class…
2
votes
1 answer

Generic Binary Search Using String

I have a generic binary search which functions properly for Integers in an Array. However, when applied to an Array of Strings it will only return at most three of the indexes correctly ([1],[2],[3]), while marking the others as nonexistent ([-1]).…
Austin
  • 195
  • 1
  • 4
  • 13
2
votes
2 answers

Transitivity property of String comparison in java

take a look at this code class StringComparator implements Comparator { @Override public int compare(String a, String b) { if (a.length() == b.length()) { return b.compareTo(a); } else { …
AbtPst
  • 7,778
  • 17
  • 91
  • 172
2
votes
3 answers

Does compareTo work with double type?

I need to make a program that uses compareTo, but I'm running into issues with it. double sides1 = 1.0; double sides2 = 1.3; int compared = sides1.compareTo(sides2); I always run into an error that says Cannot invoke compareTo(double) on the…
public static void
  • 95
  • 1
  • 6
  • 13
2
votes
2 answers

implementing compareTo (Comparable) for a Binary Tree(Generic)

I'm generating a Binary Tree with key - value nodes. It works like this: The ordering is as followed: If you implement a new node you give a key and a value(not important) it will check if there is a node already if not it will create it as the…
NhatNienne
  • 937
  • 3
  • 11
  • 20