compareto Questions

4

Solved

@Override public int compareTo(Object t) { if(t instanceof Student) { Student s = (Student)t; return (this.name.compareTo(s.name)); } else return -1; } This is my compareTo method impleme...
Nombles asked 12/9, 2013 at 7:15

4

Solved

I am writing a phonebook program in java and i need to list people in the list alphabetically and to do that i need to write a sorting algorithm for a list in java and it should use only compareTo(...
Ciracirca asked 10/12, 2014 at 20:30

2

Trying to use Java's DelayQueue, I have to implement the Delayed interface which requires a compareTo() "method that provides an ordering consistent with its getDelay method.". The intention is of ...
Usurious asked 22/6, 2014 at 16:22

1

Solved

This code executes successfully in .NET 4.0 public void CompareTest() { var m1 = new Foo { Order = 1 }; var m2 = new Foo { Order = 2 }; var c1 = new Bar { Order = -1 }; var c2 = new Bar { Or...
Penrose asked 7/7, 2014 at 12:46

4

Solved

What should be returned in a CompareTo method when the given object is null? The MSDN Library shows a example where 1 is returned. But I would have expected to throw an error because comparing to ...
Vanhomrigh asked 10/6, 2013 at 14:11

1

Solved

I've been going over some Microsoft samples of code for the Kinect sensor and have stumbled across the following line. TimeSpan zeroDuration = TimeSpan.FromSeconds(0.0); TimeSpan timeRemaining = ....
Vase asked 15/4, 2014 at 16:4

1

Solved

In answering this question, I discovered the following behaviour of compare on discriminated unions. type T = A | B | C | D compare A B (* val it : int = -1 *) compare A C (* val it : int = -2 *...
Pecten asked 14/3, 2014 at 22:52

2

I've been trying to override a compareTo in such a way: This is the original: @Override public int compareTo(ProductPart6 s) { return this.getproductName().compareTo(s.getproductName()); } thi...
Gompers asked 25/11, 2013 at 23:32

2

Solved

I am studying the source of the OpenJDK. My attention was attracted by the methods Byte.compare() and Integer.compare(): public static int Byte.compare(byte x, byte y) { return x-y; } public st...
Geneviegenevieve asked 12/11, 2013 at 8:16

4

Solved

I am implementing Comparable interface on a trivial class that wraps a single int member. I can implement it this way: @Override public int compareTo ( final MyType o ) { return Integer.valu...
Diminish asked 1/9, 2011 at 16:4

2

Solved

General question: When implementing an override of the default equals method in Java, what concerns should I have about simply utilizing an already implemented compareTo method vs writing independe...
Cavell asked 29/5, 2013 at 14:36

4

Solved

I'm trying to generate a hashCode() method for my simple class but i'm not getting anywhere with it. I would appreciate any help. I've implemented the equals() method, which looks as follows, and w...
Haldis asked 4/5, 2013 at 19:5

5

I'm well aware of the contractual needs to make sure that hashCode is consistent with equals and that equals is consistent with compareTo. However, this is often violated in practice. Are there any...
Carisa asked 8/3, 2012 at 20:1

1

like compareTo, that have to be "reflexive, antisymmetric and transitive", are there any rules to implement the compare method?? thanks
Decorum asked 6/2, 2013 at 15:24

5

I am beginner in java, I am trying to compare two strings in java char by char and find how many different chars they have by the following code but it doesn't work, min is the min between the 2 ...
Correctitude asked 5/8, 2012 at 21:54

1

Solved

I posted some code here which correctly solved a problem the poster had. OP wanted to remove duplicates and bring certain special items to the top of a list. I used a TreeSet with a special Compara...
Handful asked 6/10, 2012 at 16:25

2

Solved

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<String>() { public int com...
Heaton asked 12/9, 2012 at 21:39

3

Solved

When compiling the code below, I get the following error: PersonalInformation is not abstract and does not override abstract method compareTo(Object) in Comparable I assume that means I have...
Incapacity asked 20/2, 2012 at 14:21

3

Solved

I'm having a strange problem comparing strings. I send a string to my server (as bytes using getBytes()) from the client. I've ensured that encoding is the same on the client and server by starting...
Plaid asked 1/10, 2010 at 17:12

6

Solved

If I have a class Person that implements Comparable (compares personA.height to personB.height, for example), is it possible to use personA < personB as a substitute for personA.compareT...
Noisette asked 20/1, 2012 at 18:25

2

Solved

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 o...
Cellulitis asked 16/1, 2012 at 20:31

3

Solved

I am trying to compare objects in an object[] that are of a single type (unknown at runtime). They are of System.string, int, decimal, Datetime, or bool types. Is there a way to compare two of the...
Roadstead asked 22/11, 2011 at 20:46

5

Solved

I am clueless here... 1: private static class ForeignKeyConstraint implements Comparable<ForeignKeyConstraint> { 2: String tableName; 3: String fkFieldName; 4: 5: public int compareTo(...
Muscarine asked 9/4, 2010 at 16:15

6

Solved

Let's have a class Person. Person has a name and height. Equals and hashCode() takes into account only name. Person is comparable (or we implement comparator for it, does not matter which one). Per...
Jorge asked 29/8, 2011 at 12:19

1

Solved

The following little test throws an NPE: public class Test { public static void main(String[] args) { String a = "a"; String b = null; System.out.println(a.compareTo(b)); } } Yet, the ...
Survance asked 23/8, 2011 at 22:55

© 2022 - 2024 — McMap. All rights reserved.