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
1
vote
1 answer

compareTo method won't compile

My class header: public class GraphEdge implements Comparable{ /** Node from which this edge starts*/ protected Point from; /** Node to which this edge goes*/ protected Point to; /** Label or cost for this edge*/ protected int cost; My…
Colleen
  • 23,899
  • 12
  • 45
  • 75
1
vote
1 answer

Clashing Bookings Comparing DateTimes using CompareTo

I am making a booking system where users can make appointments with each other. What I need to do is make sure that the meetings do not clash time wise. For example: Meeting 1 is from 13:00 to 14:00. Meeting 2 is from 13:30 to 14:30. These meetings…
Mac
  • 382
  • 2
  • 8
  • 26
1
vote
3 answers

Implementing Icomparable Interface for generic class

I am not able to find out as to how to implement the IComparable interface method CompareTo for a generic class. I have a class called BindingProperty which is used to create a List> to bind to a DataGrid. The…
Shakti Prakash Singh
  • 2,414
  • 5
  • 35
  • 59
1
vote
3 answers

Unexpected behavior with PriorityQueue remove: Why isn't compareTo used?

I am trying to use the priority queue, but the remove() is not working: My code: PriorityQueue pq=new PriorityQueue(); OwnClass a=new OwnClass(1); OwnClass b=new OwnClass(2); OwnClass c=new…
TimeToCodeTheRoad
  • 7,032
  • 16
  • 57
  • 70
1
vote
2 answers

Difference between Collator (locale-sensitive) and compareTo (lexicographically) for comparing String values

I've been reading about using Collator and the compareTo method in String for comparing Strings. I'm unsure what the real difference is between the two from reading the API. When is one to prefer over the other? API Collator API String compareTo
Teletha
  • 603
  • 1
  • 11
  • 21
1
vote
1 answer

How do I calculate the number of Mondays since the start of this year?

I'm trying to write a simple procedure that will add to a List every second Monday (every pay period day) since the beginning of the year, what is happening is kind of strange but I've never worked with the Calendar class before and I think I may be…
davidahines
  • 3,976
  • 16
  • 53
  • 87
1
vote
2 answers

PowerShell version number comparison and leading zeros

I'm trying to compare a couple of version numbers with a simple PowerShell example. From the below, I would expect $thisversion to be less than $nextversion. But the comparison suggests not? What am i missing? I'm gathering that [version] treats…
Captain_Planet
  • 1,228
  • 1
  • 12
  • 28
1
vote
0 answers

comparing generic types java, compareto override

i'm trying to run this but i can't compare that type with < or > it gets this error: The operator > is undefined for the argument type(s) T, T, anytips on how to fix it???????????????????????????? public class GenericElement>…
1
vote
3 answers

Sorting an array based on different properties of objects by overriding compareTo() method

I have a list of Student objects, each of them has 3 properties. Color, Size and their names. I want to sort them in ascending order based on their color first, followed by descending order of size and finally, in ascending order of their names. For…
Sammie
  • 17
  • 5
1
vote
3 answers

private instance variable accessible with "public" scope inside compareTo

Strangely, instance variable brand is private scope, yet accessible the "public" way inside of method compareTo. public class Car implements Comparable { private String brand; public String getBrand() { return brand; } …
Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
1
vote
2 answers

Confusion caused by inner compareTo() method in the @Override

@Override public int compareTo(StockItem o){ if(o != null) return this.itemName.compareTo(o.getItemName()); } I know, it is fairly simple to many of you, yet I would like to clear the doubts. At…
max
  • 166
  • 13
1
vote
1 answer

Using compareToIgnoreCase() to compare an entry to substring in string without using array

In my assignment it's forbidden to use collections and any arrays. We are allowed to use String Tokenizer but any other classes than String and System are not allowed. This solution has to work with any number of entries. I have a string which look…
AlexCorDev
  • 55
  • 6
1
vote
3 answers

Creating compareTo method in java with one parameter

I have a question about making a compareTo function in Java. In Java, we have the String.compareTo(String) method. However, I need to make a compareTo function with only only parameter, like: compareTo(String). I assume that I need to use this to…
NBB
  • 137
  • 2
  • 6
  • 14
1
vote
1 answer

c# CompareTo not behaving as expected along with SortedSets

I'm storing users table-column-configuration in a simple class: public class ColumnUserSetting : IComparable { public String TableWrapperName { get; set; } public String ColumnName { get; set; } …
dognose
  • 20,360
  • 9
  • 61
  • 107
1
vote
1 answer

Why is my compareTo() method giving error

class pair{ int start; int end; pair(int start, int end) { this.start = start; this.end = end; } } public class Solution { public void intervals(ArrayList arrive, ArrayList depart) { …