aspose file tools*
The moose likes Cattle Drive and the fly likes OOP-3 Sortnames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "OOP-3 Sortnames" Watch "OOP-3 Sortnames" New topic
Author

OOP-3 Sortnames

Dan Rh Jones
Greenhorn

Joined: Aug 16, 2011
Posts: 19
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

Joined: Oct 01, 2001
Posts: 2851
    
  11

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

Joined: Aug 16, 2011
Posts: 19
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

Joined: Sep 13, 2007
Posts: 2419
    
    7

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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Jack Moore Iii
Ranch Hand

Joined: Jun 07, 2012
Posts: 76
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

Joined: Aug 16, 2011
Posts: 19
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

Joined: Oct 01, 2001
Posts: 2851
    
  11

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: OOP-3 Sortnames