Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2008

Tip: Looking for answers? Try searching our database.

CompareTo for Objects

Thread view: 
Andrew Marcus - 24 Mar 2008 19:49 GMT
Can anyone help?All I want to do the following in one of my program:-

Object p=.................;//some object corresponding to my
program.But I couldn't do

if(p.compareTo(object k)<=0)
   .................

CompareTo methods is for integer but I want to do it for objects,what
should I do to handle that problem??
Alex.From.Ohio.Java@gmail.com - 24 Mar 2008 20:11 GMT
> Can anyone help?All I want to do the following in one of my program:-
>
[quoted text clipped - 6 lines]
> CompareTo methods is for integer but I want to do it for objects,what
> should I do to handle that problem??

You can measure them them in any way where result is int - color,
size, time of creation, level of love, distance from hell. Anything is
good. Only one thing is required - result should be stable. It's not
appropriate if in one comparison one object is bigger them second one
and then, in another comparison, they are equal.

Alex.
http://www.myjavaserver.com/~alexfromohio/
Jeff Higgins - 24 Mar 2008 20:39 GMT
> Can anyone help?All I want to do the following in one of my program:-
>
[quoted text clipped - 6 lines]
> CompareTo methods is for integer but I want to do it for objects,what
> should I do to handle that problem??

class YourObject extends Object {

 public YourObject() {

 }

 public int compareTo(Object that) {

 if(this < that)
   return -1;
 else(this > that)
   return 1;
 return 0;

 }

}
Eric Sosman - 24 Mar 2008 21:12 GMT
> Can anyone help?All I want to do the following in one of my program:-
>
[quoted text clipped - 6 lines]
> CompareTo methods is for integer but I want to do it for objects,what
> should I do to handle that problem??

    You have (at least) three problems here, two technical
and one conceptual.

    The first technical problem is simple: Object does not
have a compareTo method.  It may well be the case that the
object `p' refers to comes from a more specialized class
that does in fact implement compareTo, but you haven't let
the compiler know about it.  By declaring `p' as a reference
to plain-vanilla unspecialized Objects, you have told the
compiler that only the methods of the Object class itself
are accessible through `p'.  Solution 1: Declare `p' as a
reference to the more specialized class you're actually
using.  Solution 2: Declare `p' as a reference to Comparable,
meaning that it can refer to objects from all classes that
implement the Comparable interface (and are thus known to
provide a compareTo method).  Other solutions are possible.

    The second technical problem is also simple: `object k'
is not valid as an argument list in a method call.  If you
mean that `k' refers to an Object (note the capitalization,
by the way), just write `p.compareTo(k)'.  As in the first
case, it may make more sense to declare `k' as a reference
to something more specific than plain Object.

    The conceptual problem is harder to cope with, because
I'm not sure just where it originates.  You say "CompareTo
methods is for integer," and it's not clear to me just what
you mean by this.  Easy things first: The method you're
talking about is compareTo, not CompareTo or compareto or
comPareto; letter case has meaning in Java.  But it's the
second part of the claim that puzzles me.  I imagine that
you might mean

    - compareTo returns an int value -- true -- and that it
      is therefore only applicable to integer values.  That's
      not the case!  compareTo compares one object to another
      and uses an int value to report the outcome: a negative,
      zero, or positive result to indicate that `this' compares
      "less than," "equal to," or "greater than" the argument.
      The int value returned from compareTo is just a way of
      encoding what compareTo discovered internally.  It is
      as arbitrary as "One if by land and two if by sea," and
      its int-ness implies nothing about the nature of the two
      objects being compared.  You can write a Planet class
      that compares Earth to Mars on the basis of their mean
      distance from Sol; the returned value will be a negative
      int (Earth is closer than Mars), but it does not follow
      that either Earth or Mars is an int!

    - compareTo works with Integer objects (note the case) --
      also true.  The Integer class implements Comparable, and
      therefore it provides a compareTo method.  But Integer
      is not the only class that has compareTo!  So do all the
      other core numeric classes like Double and Short; so do
      String and Charset and Calendar and RoundingMode and a
      host of other classes.  You are perfectly free to implement
      compareTo in your own classes; it is not the exclusive
      property of Integer.

... or maybe you mean something else I haven't figured out.  If
so, you can try asking your question again and explaining more
about what troubles you -- but I suspect you may have more success
if you spend more time reading your Java textbook.

Signature

Eric.Sosman@sun.com

Roedy Green - 25 Mar 2008 05:15 GMT
On Mon, 24 Mar 2008 11:49:15 -0700 (PDT), Andrew Marcus
<mainhoonanjaane@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>CompareTo methods is for integer but I want to do it for objects,what
>should I do to handle that problem??

see http://mindprod.com/jgloss/comparable.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Chase Preuninger - 30 Mar 2008 19:54 GMT
All objects that implement the comparible interface have this.  I
think that compareTo is actualy a protected method of object.

http://groups.google.com/group/java-software-develoupment?hl=en
Patricia Shanahan - 30 Mar 2008 20:49 GMT
> All objects that implement the comparible interface have this.  I
> think that compareTo is actualy a protected method of object.

Why do you think it is a protected method of Object?

Unlike clone and finalize, which definitely are protected methods of
Object, compareTo does not appear in the Object API documentation. See,
for example, http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html

It does not even appear in the distributed source code for Object.

Patricia
Eric Sosman - 30 Mar 2008 20:57 GMT
> All objects that implement the comparible interface have this.  I
> think that compareTo is actualy a protected method of object.

    A few moments with the Javadoc should correct your thinking.

Signature

Eric Sosman
esosman@ieee-dot-org.invalid



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.