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

Sort ignoring punctuation (Java)

I am trying to sort an Android ListView object. I am currently using the following code: // Sort terms alphabetically, ignoring case adapter.sort(new Comparator() { public int compare(String object1, String object2) { …
CaptainProg
  • 5,610
  • 23
  • 71
  • 116
3
votes
1 answer

Why does List.Sort() call CompareTo(Object) rather than CompareTo(Shape)?

Earlier, I was provided a concrete example of contravariance in the generic IComparable interface by Jon Skeet. This has, however, spawned yet another question. Why isn't the generic List.Sort() method able to infer the same information? I've…
blf
  • 117
  • 4
3
votes
2 answers

A cleanly overridable compareTo() method?

Having to, for the first time, define relationships amongst related objects, I found myself spending an entire weekend scouring the web for information pertaining to cleanly overridable implementations for equals() and compareTo(). Having found very…
blf
  • 117
  • 4
2
votes
3 answers

Is there any way to implement compareTo method which would compare any supplied argument?

The compiler says " cannot find symbol: method compareTo(java.lang.Object) ". Could you please advice where is the mistake here? Here is the part of the code: public class OBTComparable implements Comparable { public…
2
votes
6 answers

Java: Is it OK to compare dates as strings

From an external service I get objects with Date+Time fields as String's in format 2012-03-07 12:12:23.547 and I need to compare these fields to get a correct order of the objects. I am well aware that I can create Date objects via e.g.…
hgus1294
  • 747
  • 14
  • 26
2
votes
2 answers

Overflowing the return result for compareTo?

Is there a potential for overflow if I were to write the following: public class SomeObj implements Comparable { private final float data; public SomeObj(float data) { this.data = data; } public int…
BJ Dela Cruz
  • 5,194
  • 13
  • 51
  • 84
2
votes
5 answers

Best way to implement compare method of Comparator in java?

I have written a comparator which sorts by ascending order as below. which is working well. Collections.sort(resultList,new Comparator() { @Override public int compare(MyModelClass o1, MyModelClass o2) { …
user1016403
  • 12,151
  • 35
  • 108
  • 137
2
votes
6 answers

Random numbers vs. GetHashCode() in CompreTo()?

I'm using the Random class in my struct's CompareTo() to pick, with equal probability, one of the structs when both have the same field values. The Random class is instantiated with a fixed seed to get a reproducible sequence of pseudo-random…
alhazen
  • 1,907
  • 3
  • 22
  • 43
2
votes
7 answers

compare more field of a object in compareTo

I'd compare more than one only field of a object using the compareTo method. Is it possible? for istance: public int compareTo(Object o) { return field.compareTo(o.field); } I create this method to sort a collection. Obviously my object has to…
Mazzy
  • 13,354
  • 43
  • 126
  • 207
2
votes
2 answers

How to compare a string column to a number (as a decimal) in Linq

I have a database with an amount field - it's a decimal, although the database schema has the column defined as a string. I want to be able to run a Linq query to find values based on a number for the amount. There are questions like this and this…
George
  • 3,251
  • 7
  • 32
  • 39
2
votes
6 answers

How to use compareTo within a Generic array in java?

I am trying to figure out how to compare two items within a T[] array, here is what I have: public static > T getLargest(T [] a, int low, int high){ if(low>high) throw new…
randomizertech
  • 2,309
  • 15
  • 48
  • 85
2
votes
3 answers

What is the difference between Compare() and CompareTo() for strings in C#?

I know it returns integer but I needed to know on what basis. Example: string s1 = "world"; string s2 = "World"; Console.WriteLine(string.Compare(s1,s2)); Console.WriteLine(s1.CompareTo(s2)); it returns -1 for both. But if we capitalize w in…
Arun
  • 25
  • 7
2
votes
1 answer

Java - Comparable interface with very small number

I have this class that stores the latitude and the longitude of a coordinate and it distances from the center. At some point in my algorithm I have a list of these Coordinate and I want to sort them by their distance from the center. Thus, I…
2
votes
3 answers

Java Comparable, mutliple/different implenetations of compareTo() method

I have this class: public class Sample implements Comparable { public String a; public String b; public String c; public int compareTo (Sample sampleToCompare) { int compResult = this.a.compareTo(sampleToCompare.a); return (compResult…
user201788
2
votes
1 answer

Java BigDecimal CompareTo Not Working with Small Numbers

I need to compare extremely small numbers for an artificial intelligence project. Here is my method: private static int maximum(BigDecimal a1, BigDecimal a2){ System.out.println(a1); System.out.println(a2); if (a1.compareTo(a2)<0){ …