• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sorting schmorting

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are comparing Strings, the last names.
 
Nick Cabell
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic