• 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

OOP-3 Sortnames

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am at a lose as to how to procedd with OOP-3 SortNames.

I am able to sort by first name easily enough but sorting by the surname is proving too much

I thought that perhaps I should implement a new Name object that has first and second names as attributes and then have setters and getters.

then do something with getting all the last names on the Name objects and ordering that

Is this something I should go for? Am i going on the right direction? Is what am I thinking even possible?

I am pretty much stuck at the moment and any advice is greatly appreciated.

 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, creating a Name class is overkill for this assignment. You just need a way to split the name strings into pieces. Take a look at the split() method in java.lang.String to see if that helps.
 
Dan Rh Jones
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Name class scrapped!

I know the split method, but not sure how to use it in this case.

Would have I have to create an array for each name in the text file and then do some magic to compare each array?



The assignment says not to create a second list, so if its not multiple arrays, (which isn't very scalable (should we care about this at the moment?)) then I have no clue how to progress! (again)
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For sorting first names, did you use the Collections.sort(nameList) approach? Without giving the answer, is there another approach to sorting to the first name? If you know the answer to that, your last name sorting will be a cake.

Oh for your question, arrays will just make it more complicated.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to sort them by surname?! Gah, and I thought I had it all down pat, hah. Time for another pass...
 
Dan Rh Jones
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:For sorting first names, did you use the Collections.sort(nameList) approach? Without giving the answer, is there another approach to sorting to the first name? If you know the answer to that, your last name sorting will be a cake.

Oh for your question, arrays will just make it more complicated.



Only just had chance to look back at this.

I did use Collections.sort(nameList) and I dont know what other approach there is. Unless you mean implement Comparator, which is what I thought about doing in my OP when I said should I create a Name class.

Any other hints would be greatly appreciated.

 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you should be using Collections.sort(). You of course could write your own sort routine, but that's not the focus of the assignment. To sort the list of names as they are, you can just use Collections.sort(list), but to sort by last names, you will have to write a custom Comparator class and call Collections.sort(list, myComparator).
 
Are we home yet? Wait, did we forget the tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic