Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sorting schmorting

 
Nick Cabell
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are comparing Strings, the last names.
 
Nick Cabell
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic