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

How to override compareTo method for doubles?

I'm currently having some trouble understanding how the compareTo method works for the Comparable class, and how to override it. I have array of pairs, each of which hold 2 double values, that I'm trying to sort. This is what I tried: static class…
2
votes
7 answers

In Java what should I use for a PriorityQueue that returns the greatest element first?

Java's PriorityQueue places the least element at the head of the list, however I need it to place the greatest element at the head. What what's the neatest way to get a priority queue that behaves like that. Since I wrote the class stored in this…
Benjamin Confino
  • 2,344
  • 3
  • 26
  • 30
2
votes
3 answers

compareTo() method throwing "This method must return int" error despite returning int in method

I'm working on an assignment for my computer science course requiring me to implement a Comparable interface. We haven't discussed the interface at any sort of length except just being told it compares two objects and returns less than, greater than…
GainzNerd
  • 312
  • 1
  • 10
2
votes
3 answers

Compare by multiple methods in java compareTo?

I don't think that's the best way to word that title but I can't think of a better way to word it. Here's my problem: I have to write a method that compares in several different ways. If the last names are the same, I then need to compare by first…
2
votes
1 answer

Overriden compareTo() not recognised in priority queue

Ive implemented compareTo() in a Disk class and even though it works fine when i use it in main, it gives me the following error when i try to compile the priority queue which is using the same method: MaxPQ.java:113: error: bad operand types for…
2
votes
2 answers

How can I override the compareto method to order Employees's salary?

I have 2 classes: person and employee and an interface Human Within my Person class, I have a compareTo Method (Human h) Which assigns the +1,-1 and 0 for the person's age. My class employee = public class Employee extends Person implements Human…
Anika
  • 103
  • 1
  • 8
2
votes
3 answers

When using .compareTo to compare dates, why doesn't it take Months into account?

I'm displaying certain dates from an ArrayList and using a formatter to shorten the date to "dd/MM" but when I compare the dates it doesn't work for the months. e.g. 17/08 IS before 19/08, 21/08 IS after 19/08 but 17/09 IS before 19/08. What my…
2
votes
4 answers

compareTo() and Collections.sort() Solution For Multiple Column Ordering (Ascending) question

Am somewhat confused with Java's compareTo() and Collections.sort() behavior. I am supposed to sort a column in ascending order using compareTo() & Collections.sort(). My criteria is (if the same number occurs than please sort the next available…
PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
2
votes
2 answers

How to implement compareTo for all implementations of an interface?

In Java, what is the best approach to provide natural ordering for all implementations of an interface? I have an interface, for which I want to ensure/provide natural ordering between all implementations by extending the Comparable…
amaidment
  • 6,942
  • 5
  • 52
  • 88
2
votes
2 answers

How to order substring lexographically

I want to order the substring of string 's' lexographically of length 'k' I have tried to first sort the characters of the string lexographically using comapareTo function and then have tried to print the first and last substring public static…
2
votes
4 answers

java compare to from double to int tostring method

How do i go about using compareto for a double and i want to turn it into an int? An example would be nice. I have been searching the java api. also is it possible to use if and else statements with a String toString method? Mine has kept on…
Cferrel
  • 321
  • 2
  • 5
  • 19
2
votes
1 answer

Quick Sort (compare to) in Java

This program cannot run but I don't know why: Abstract class public class Mahasiswa implements Comparable { private String nama; private int nim; public Mahasiswa(String nama, int nim) { } public String getNama() { …
2
votes
2 answers

Compare Array of Strings and return the smallest one

I am implementing a code to Compare Array of Strings and return the smallest one using stringONE.compareTo(stringTwo). The code will use recursion to alternate through the Array instead of a for loop. I implemented the code so that the compare…
PonzuShusi
  • 21
  • 1
2
votes
3 answers

How to sort LinkedLists using compareTo in java8

I need to sort a linked list of StudentInfo objectsx using compareTo(). This is my StudentInfo clas package mahaffy_lab4; import java.util.*; /** * * @author student */ public class StudentInfo implements Comparable { private…
I. McFee
  • 33
  • 4
2
votes
4 answers

How to compareTo or Count Difference from two date in android studio

i already tried to compareTo two date in android studio like this String dtThen = "13-10-2013"; Date dateThen = formatter.parse(dtThen); String dtNow = "15-10-2013"; Date dateNow = formatter.parse(dtNow); if…
Megi Fernanda
  • 263
  • 1
  • 4
  • 15