Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OOP-3 Sortnames

 
Dan Rh Jones
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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.

 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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)
 
K. Tsang
Bartender
Posts: 3447
13
Android Java
  • Mark post as helpful
  • send pies
  • 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.
 
Jack Moore Iii
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic