This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes sorting schmorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "sorting schmorting" Watch "sorting schmorting" New topic
Author

sorting schmorting

Nick Cabell
Greenhorn

Joined: Jun 15, 2002
Posts: 11
I've read all the postings on this most popular lesson (OOP-3) and believe it or not, I still can't get it. I guess you guys have done a good job of saying all sorts of great stuff without giving it away.
I tried not to reveal too much here (censors get ready) but I figure how much can I give away when I'm so confused
My question does not seem to have come up exactly. I understand there is a sort method that allows you to sort according to a different logic than natural order. I understand that this sort() requires a certain object type. (Am I correct that this object is really just one that's "implements" the Comparator i'face?) I've also looked at the method that my object needs to implement. What baffles me is what I am comparing. I just can't see how comparing two objects helps me sort by last name.
I guess this is the challenge of the lesson so maybe someone who's done it will have mercy and just give me hint where to start looking or how to look at this differently than I am.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
You are comparing Strings, the last names.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Nick Cabell
Greenhorn

Joined: Jun 15, 2002
Posts: 11
But what do I compare each last name to? Would I iterate through the list as it is and compare the first item to the second, the second to the third, etc? That doesn't make sense to me how that would accomplish the sort.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1199
Ah, this one spun me in circles too. The sorting is accomplished by the methods that come with the interface you're using, so you don't have to figure out how it's done, just select the method you want, feed it the list to be sorted, and sit back and be confused!
I hope this is an accurate description. I found this very disturbing because I have a "How the heck does it do that???" kind of mind. If I don't understand how something works, I don't really get it, even if I get my code to work!
Good luck!
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
You asked, "Am I correct that this object is really just one that's "implements" the Comparator i'face? ... What baffles me is what I am comparing."
Marilyn said (in her special, succinct way ), "You are comparing Strings, the last names."
Implicitly, she answered yes to your first question.
I did a quick search here using "comparator" and came up with some posts that really helped me with this one.
Hang in there,
Pauline
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1757
    
    3
Nick,
You mentioned that you read through old posts on OOP-3 and that didn't help clear things up.
Keep in mind that you can revive those old threads if you want. For example, if there's a post in one of those old threads that has something you'd like clarification on, just add a reply to that thread with your question and the thread will pop to the top of the list.
(But I guess if you have questions about multiple threads it might make more sense to centralize the discussion here and use URL tags to include links back to specific older posts you have questions about.)
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1757
    
    3
Originally posted by Nick Cabell:
But what do I compare each last name to?

< cryptic >Other last names, of course.< /cryptic >
Would I iterate through the list as it is and compare the first item to the second, the second to the third, etc? That doesn't make sense to me how that would accomplish the sort.

The actual list sorting method (that's part of Java -- not something you have to write yourself) takes care of iterating through the list and making sure that a sorting rule is called for every necessary pair of objects in the list. The gory details of that process are hidden from you.
For the method that you write (the one in the class that implements the interface), you are essentially defining a sorting rule that tells the Java-supplied sorting routine what to do with any arbitrary pair of objects it happens to pull out of the list for comparing.
[ November 05, 2002: Message edited by: Michael Matola ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sorting schmorting