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 / General / March 2008

Tip: Looking for answers? Try searching our database.

CompareTo for Objects

Thread view: 
Andrew Marcus - 24 Mar 2008 19:51 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:06 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??

compareTo only measures difference in integer.
It's like "in scale from one to ten how do you like this song?"
That's entirely up to you (your objects) in which way or how you
measure them. Only difference should be expressed in int.

Alex.
http://www.myjavaserver.com/~alexfromohio/
Matt Humphrey - 24 Mar 2008 20:33 GMT
>> Can anyone help?All I want to do the following in one of my program:-
>>
[quoted text clipped - 11 lines]
> That's entirely up to you (your objects) in which way or how you
> measure them. Only difference should be expressed in int.

The contract for compareTo is here with the Comparable interface
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html

Essentially compareTo expresses the order of two things (anything that
declares it has an order--e.g. is Comparable) by returning a negative value
if the first precedes the second, a positive value if the second precedes
the first and 0 if they are equal. You can make up an ordering for any kind
of object you like.  I also strongly recommend that the equals method be
compatible with compareTo--I've been tripped up when they're not. There's no
requirement that the magnitude of compareTo yield the distance between the
items.

The real question here is how to define the ordering function and that
depends entirely on what's in the object.  For example, if an object has
   String lastname
   String firstname
   int age

you can write an ordering function that goes age, lastname, firstname like
this so that it compares the various fields in order of importance.  If the
fields are different, stop and return an answer.  If they're the same
(yields 0), go test the next field.

public int compareTo (Object o) {
 MyObject myObject = (MyObject)o;
 if (age < o.getAge()) return -1;
 if (age > o.getAge ()) return 1;

 int c = lastname.trim().toLowerCase().compareTo
(o.getLastName().trim().toLowerCase());
 if (c != 0) return c;

 return
firstname.trim().toLowerCase.compareTo(o.getFirstName().trim().toLowerCase());

Matt Humphrey http://www.iviz.com/
Hal Rosser - 25 Mar 2008 00:02 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??

If you had an array of Customer objects, you could use the sort method of
the Arrays class to sort those objects (but only if the Customer class
implements the Comparable interface). But the question is how would YOU sort
Customers?? By last name? by age? by account balance? -You decide how to
compare the objects and return -1 or 1 or zero in the method. (Actually it
requires a negative int,  a positive int or zero int. - but usually folks
return -1, 1, or zero). Don't be afraid to write a few lines of code in the
method.
Matt Humphrey's response is spot-on.
Lew - 25 Mar 2008 03:19 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??

Oh, you got such good help in both groups to which you multi-posted.  It is
somewhat rude to those helpful folks that you multi-posted, though.  You
should at worst have cross-posted, but really one of the groups would have
sufficed.

Signature

Lew



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.