• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Comparable.  RSS feed

 
Gary Charles
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a homework assignment to add a compareTo() method to an existing set of classes and use a supplied JUnit test. In this example I'm trying to sort by Consultant name in TimeCardListUtil.java. As you'll see, Consultant is an object of TimeCard and Consultant consists of a Name object. I'm getting lost in the objects. In my comparison, I don't know how to get at my string to do the comparison. Although our instructions are not explicit, I think it's assumed we'll sort by alpha last name, but the first name and last name start with the same letter so it's probably not critical. I apologize for such a large example, I've paired it down as much as I could while still keeping it workable.

Here are my classes.






The comparison needs to occur right above here in Consultant.compareTo(), right?

Gary
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Charles wrote:The comparison needs to occur right above here in Consultant.compareTo(), right?


Yes, that's right. Only the code you wrote so far:



isn't right. But you already knew that, no? Your question is what code to put in there instead.

I assume you want to have Consultant A come before Consultant B if A's name comes alphabetically before B's name? If so then the first thing to do is to get those names:



Then you have to write code which compares those two Name objects and returns negative, 0, or positive depending on which comes first alphabetically. To do that, you'd get the data from them -- last name, first name, or whatever -- and start doing the comparisons. Compare whichever field is more important first; if you don't get 0 then you're done, return what you got. If you do get 0 then the most important fields are the same and you have to move on to the next most important field.

And remember that String also implements Comparable, so String.compareTo(String) does the work you need done when you get down to that level.

Hope that helps -- give it a try and let's see what you get.
 
Gary Charles
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul.

For Consultant.java there are a couple of options I can use to get the name. I put both in my method below and I think they are essentially the same.
I'm choosing to sort on the last name just to get something working although I would eventually like to understand how to use both last and first.

I have not been able to test this because I don't know how to call Consultant.compareTo() from TimeCardListUtil.java.
The provided JUnit test indicates

which I interpret as the sorting needs to happen with out returning anything. I've seen examples on the www that use

So of course I tried that and it does nothing. So I also tried the enhanced for loop as below.

Either way I am not communicating with Consultant.CompareTo() since a simple System.out.print statement doesn't output anything.

Gary
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Charles wrote:
So of course I tried that and it does nothing.


Ask yourself why sorting a collection of TimeCard objects should need to compare two Consultant objects. Wouldn't it be more reasonable to expect it to compare two TimeCard objects? And when it does that, what method of what class would it use?
 
Gary Charles
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for trying to spoon feed me. I know that takes a certain amount of patience. Unfortunately, I guess I need a smaller spoon.
Ask yourself why sorting a collection of TimeCard objects should need to compare two Consultant objects.

Because a TimeCard contains a unique Consultant object and Consultant is the item we're trying to compare.
Wouldn't it be more reasonable to expect it to compare two TimeCard objects?

This statement seems to be equally valid to me as the last statement. Although I think you're trying to make the point that this is the correct way to think about this problem.
And when it does that, what method of what class would it use?

Getting to object I need to use for comparison seems simple in isolation. That's where I was thinking I need to do with the enchanced for loop. In my mind the chain is something like

except you cannot operate directly on a list like this. Well, I take that back because Arrays.sort(timeCards) does.

So I am not sure where to go from here.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your comments suggest you sort of know what code has to be written. Problem is, you haven't written any of the code to support those comments.

Question: You're sorting a collection of TimeCard objects. So what compareTo method will be called?

Answer: The compareTo method of TimeCard.

Question: Does the compareTo method of TimeCard do anything like what you said, about TimeCard containing a unique Consultant object and comparing two of them?

Answer: No. In fact it is programmed so that all TimeCard objects appear to be equal.

Question: So how could you change the compareTo method of TimeCard so it compares the Consultant objects which belong to the two TimeCard objects?
 
Gary Charles
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. That really helped.
The assignment instructions noted that I needed to implement the Comparable interface in TimeCard so I had the empty method, but I thought the requirement was for a different part of the assignment. Now that you've given me the hint, it(somewhat) makes sense that you have to daisy chain the compareTo() methods just like other object calls where the item to retrieve is several layers deep. Once I spend time going through it I think I'll get it.


This part still seems a little bit like smoke and mirrors but some of that has to do with using static methods. It's like the list is sorted in place which is confusing to me. But that's another topic.







Here's my code for reference in case it helps someone else.




Gary
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Charles wrote:It's like the list is sorted in place which is confusing to me.


It is indeed sorted in place when you use Collections.sort(). If you find that off-putting, imagine that you are given a tray with little compartments containing numbered scraps of paper, and you have to put the scraps of paper in order. You might do that by looking at pairs of scraps, comparing them, and switching them if they are in the wrong order. That's essentially how Collections.sort() works, only with extra computer science research which works out how to do it with not too many comparisons.

Anyway it looks like you're on the right track now.
 
Gary Charles
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Message that was here removed. I found my answer.

Gary
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!