aspose file tools*
The moose likes Cattle Drive and the fly likes Implementing Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Implementing Comparable" Watch "Implementing Comparable" New topic
Author

Implementing Comparable

Pres Brawner
Ranch Hand

Joined: Jan 18, 2001
Posts: 92
I'm trying to do Assignment 3.3 and I've hit a snag.
I can:
load the ListArray from the file
print the list in First name Order.
I can't print the name in last name order. I'm trying to implement Comparable in my class. This requires that I override the compareTo method.
I would have thought when I perform a sort(c) where c is my Collection, I would use the overridden compareTo function. Instead, it uses the default behavior (first name order). I'm not even reaching my overridden version of compareTo (I have a System.out.println statement that never gets processed.)
Any ideas?
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Pres,
I'm not up to that assignment yet but I checked the class libraries for Comparable and found the following info:

Most sort routines (for example, Collections.sort()) have two forms, one that takes a Comparator and one that does not. If you use the form that takes the comparator, the sort method uses the comparator to order the elements. If you use the form that does not take a comparator, the sort method requires that all the elements implement the Comparable interface. In particular, by implementing this interface the elements provide a compareTo() method that the sort method calls to determine the ordering.

Which, I think, means the element type of your collection needs to be the one that implements Comparable and overrides compareTo().
Hope I'm not sending you on a wild goose chase.
Jane


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
You have to make your own comparator object that implements Comparable and overrides compareTo().


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Pres Brawner
Ranch Hand

Joined: Jan 18, 2001
Posts: 92
Thanks, I'll try to make a new comparator object.
Nick Cabell
Greenhorn

Joined: Jun 15, 2002
Posts: 11
Originally posted by Marilyn deQueiroz:
You have to make your own comparator object that implements Comparable and overrides compareTo().

Still completely confused on OOP-3. Michael M says to post a reply anywhere you need the clarification. Here's where I am stuck right now:
I've got that I need to create a Comparator type object so I can use it with the appropriate sort() method. But does this object have to ALSO implement the Comparable interface? That means it implements both interfaces and three methods?
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
I've got that I need to create a Comparator type object so I can use it with the appropriate sort() method. But does this object have to ALSO implement the Comparable interface? That means it implements both interfaces and three methods?
Hopefully my question will clear up some confusion for you. Will the object that is performing the comparison logic need to be compared to other objects?


Matthew Phillips
Nick Cabell
Greenhorn

Joined: Jun 15, 2002
Posts: 11
I think my Comparator object (that's the one I give to sort to tell him how to sort differently, right?) really doesn't need to be compared to other objects. He just needs to include the logic so that the sort method knows what rule to use on the collection. Thye sort knows that he is comparing all the elements in his list.
If I am correct, I would think I don't need to override the methods in Comparable. But this leads me to another question which is how do I make a comparator object without implementing the interface. It's not an object so SOMEONE has to implement it.
Thanks for asking.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1752
    
    2
Just to clear up one thing --
In her post of January 23, 2001, Marilyn makes a rare, rare slip of the tongue.
You have to make your own comparator object that implements Comparator and overrides compare().
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1752
    
    2
Originally posted by Nick Cabell:
I think my Comparator object (that's the one I give to sort to tell him how to sort differently, right?) really doesn't need to be compared to other objects. He just needs to include the logic so that the sort method knows what rule to use on the collection. Thye sort knows that he is comparing all the elements in his list.

Yes, exactly.
...how do I make a comparator object without implementing the interface.

This particular thread is adding to confusion, I think, because it confuses Comparable and Comparator (and compareTo() and compare()).
Looks to me like you're starting to get the right idea. See my post that immediately precedes this one.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Originally posted by Nick Cabell:
If I am correct, I would think I don't need to override the methods in Comparable.


True. (Thanks, Michael)

But this leads me to another question which is how do I make a comparator object without implementing the interface. It's not an object so SOMEONE has to implement it.

The comparator object does implement the Comparator interface.
Nick Cabell
Greenhorn

Joined: Jun 15, 2002
Posts: 11
My apologies - even in my questions I reversed the two classes at one point. My comments are confusing because I thought I was having to create a Comparator object that also implemented the Comparable interface.
Thanks for clearing that up.
So I am back where I started weeks ago trying to figure out how to do this with the Comparator interface. My question that Mathew answered had been assuming I was using the compareTo() method. I was happy with the compareTo() method because it has the more intuitive one argument. It is the two arguments of the Compare() method that throw me off.
Someone give me a hint: Does the answer have to do with several of the methods that Interators often use when going through a list? That's my stab in the dark.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1752
    
    2
Originally posted by Nick Cabell:
I was happy with the compareTo() method because it has the more intuitive one argument. It is the two arguments of the Compare() method that throw me off.

It shouldn't. It's the same idea at the core. With compareTo() there are two objects being compared -- "this" and the argument. With compare() there are also two objects being compared -- the two arguments. And in both cases the methods return an integer to indicate the result of the comparison.

Someone give me a hint: Does the answer have to do with several of the methods that Interators often use when going through a list? That's my stab in the dark.

I'm not at all sure I understand what you're getting at here, so my tentative response would be "no."
Have you by any chance looked at the Object Ordering trail on Sun's site? The examples are unnecessarily complicated because they use various nested/anonymous classes, but they do show how easy writing real compareTo() and compare() methods can be.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
You don't need to worry about the Iterator methods. The Collections' method takes care of the iteration details for you.
[ November 12, 2002: Message edited by: Marilyn de Queiroz ]
Nick Cabell
Greenhorn

Joined: Jun 15, 2002
Posts: 11
Oh my God that is SOOOO simple! I took a look at the Java trail as Michael recommended. Then one morning lying in bed it came to me what I was supposed to do.
Now that I see it I can't see anymore why the struggle was so great.
Thanks for all the pointers everyone.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
Good for you Nick. I recall having the same kind of Eureka moment with this particular subject too. It doesn't seem like it should be that simple, does it?
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1752
    
    2
Right. The hard part about this assignment is getting to the point at which you understand how easy the solution is.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Implementing Comparable