compareto

Comparing .Net Version instance using CompareTo does not work as expected.

The Version class in .Net does not implement the CompareTo interface as I would expect, it seems to handle the compare alphanumerically instead of comparing the four numbers. Maybe not a bug, but a 'feature'. Can anyone shine a light on why the compare (and also the standard <, = and > operators) do not work as I would expect below? ...

Comparing generic fields

I have some generic types, like the following: public struct Tuple<T1, T2> { ... } public struct Tuple<T1, T2, T3> { ... } etc. These should in theory be able to compare themselves against other values of the same type, so that I can write the following type of code: List<Tuple<Type, String>> l = new List<Tuple<Type, String>>(); l.Ad...

How to simplify a null-safe compareTo() implementation?

I'm implementing compareTo() method for a simple class such as this (to be able to use Collections.sort() and other goodies offered by the Java platform): public class Metadata implements Comparable<Metadata> { private String name; private String value; // Imagine basic constructor and accessors here // Irrelevant parts omitted...

Why is compareTo on an Enum final in Java?

An Enum in Java implements the Comparable interface. It would have been nice to override Comparable's compareTo method, but here it's marked as final. The default natural order on Enum's compareTo is the listed order. Does anyone know why a Java Enum has this restriction? ...

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 queue I could simply reverse the results of compareTo, its not used outside this queue. H...

Java compareto for String and Integer arguments

I am building a bubble sort 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 and the strings. I am getting some incorrect answer when using compareto to compare the casted integers. What am I doing wrong? ...

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? ...

Java Strings: compareTo() vs. equals()

When testing for equality of strings in Java I have always used equals() because to me this seems to be the most natural method for it. After all, its name already says what it is intended to do. However, a colleague of mine recently told me had been taught to use compareTo() == 0 instead of equals(). This feels unnatural (as compareTo()...

comparing a Node and an Object with a BST data structure

I'm getting a "java.lang.string cannot be cast to node" exception. I thought of converting the localRoot to a string using a provided toString method then comparing them, nut this leaves no concept of 'greater than' or 'less than' which I need to navigate the BST... int computeResult = ((Node<E>)o).compareTo(localRoot); where o is o...

equivalent of SQL binary relative value test in C#

I am trying to figure out the equivalent C# for this SQL: @var1 = "1a1" @var2 = "1F" CONVERT(varbinary, @var1) > CONVERT(varbinary, @var2) Is it the same as this? if (var1.CompareTo(var2) > 0) { } If not, then how do I simulate it? ...

Java Comparable Interface compareTo method...

I don't see anything that I am doing wrong, but NetBeans gives me the following error: incomparable types required: boolean found: java.lang.Object public int compareTo(Object obj) { if( obj instaceof Employee){ Employee employee = (Employee) obj; if(this.weekly_earnings > employee.weekly_earnings) return...

Using CompareTo() on different .NET types (e.g. int vs. double)

Hi, I've got a static method that accepts two object type variables and runs the CompareTo() method: public static int Compare(Object objA, Object objB) { return (((IComparable)objA).CompareTo(objB)); } Problem is that CompareTo() throws an exception when trying to compare between different types (e.g. int and double). Does any one...

How to CompareTo two Object without known about their real type

I have to implement a one linked list but it should put object in appropriate position. Everything was OK when I use it in conjunction with specific class, but when I tried make it universal and argument of method insert was Object some problem appeared. When I want to input Object in right position I should use CompareTo method, but the...

How do I sort an array of Person Objects by using compareto()?

Here is my code: > import java.util.Scanner; import java.util.Arrays; /** This class tests the Person class. */ public class PersonDemo { public static void main(String[] args) { int count = 0; Scanner in = new Scanner(System.in); boolean more = false; Person first = null; Person last = null; ...

FindBugs - how to solve EQ_COMPARETO_USE_OBJECT_EQUALS

I am clueless here... 1: private static class ForeignKeyConstraint implements Comparable<ForeignKeyConstraint> { 2: String tableName; 3: String fkFieldName; 4: 5: public int compareTo(ForeignKeyConstraint o) { 6: if (this.tableName.compareTo(o.tableName) == 0) { 7: return this.fkFieldName.compareTo(o.fkFieldName); ...

Does the specific signed integer matter when implementing compareTo in a Comparable <Type> class?

When implementing compareTo(), does the degree of "difference" need to be taken into account? For instance, if I have 3 objects, C1, C2, and C3, such that C1 < C2 < C3. Should C1.compareTo(C2) return an integer that is less than C2.compareTo(C3)? The documentation for the Comparable interface doesn't seem to specify one way or anothe...

Which usage of compareTo method is more understandable?

I want to sort objects based on Boolean values and I want to sort true values before false values. Which of these implementations of compareTo is more readable? Using -1 to change default behavior public class Example implements Comparable<Example>{ Boolean isOk; public int compareTo(Example o) { return -1 * this.isOk.com...

Implementing custom comparison with CustomComparison and CustomEquality in F# tuple

Hello folks! I'm here to ask a specific topic - I really found few info about this on the web. I'm implementing a F# version of Minimax algorithm. The problem I'm having now is that I want to compare Leaf of my tree (data structure below). Searching the erros the VS gave to me I arrived to something like this: The tree type I used to h...

VB6 Object Comparison

What VB6 method allows two custom objects of the same type (defined in a class module) to be compared to each other? I think there's an equivalent to Java's compareTo method, but I can't find it anywhere. ...

how to customize compareTo method to consider both direction flow

if I need to customize my code with this logic if this.srcAddr=other.srcAddr or this.src.Addr = other.sdstAddr this.srcPort=other.srcPort this.srcPort=other.dstPort because I am going to consider bi-directional flow, a packet from source to destination and a packet from destination to source belong to a flow. How should I change my c...