• Post Reply Bookmark Topic Watch Topic
  • New Topic

Methods for an ArrayList of Names  RSS feed

 
Piotr Weglarz
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What I'm trying to do is come up with a class that can do a number of things with an ArrayList of names:

1) Sort the ArrayList telephone directory style (last name, middle name, first name)
2) Sort the ArrayList using usual English form (first name, middle name, last name)
3) Be able to handle all types of names ( 1 word names such as Cher, multiple word names such as George Herbert Walker Bush, and names with embedded blanks such as Olivia de Havilland)
4) Make sure the name can fit on a standard mailing label (48 characters).

This is what I've done so far:

I made an ArrayList of objects, and each object takes a firstName String, middleName String, and lastName String. (For names like Cher, i left the middle and last name as blanks:" ".
I then made a toString method that returns lastName, middleName, and firstName (in that order). With the collection sorting ability of ArrayLists, I think I've taken care of #1.
This is where I'm stuck. How can I get toString to now satisfy #2 by returning firstName, middleName, lastName (instead of lastName, middleName, firstName)?

Am I on the right track, or is there a much better way to do this?
Any help would be greatly appreciated
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List,%20java.util.Comparator%29\

Using a Comparator for sorting would help you on #1 and #2.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use custom java.util.Comparator objects for your class, then sort using Collections.sort. That will be able to solve issues 1 to 3. I said objects (multiple) because you'll want one for sorting on telephone number, one for sorting on names, etc.

As for issue 4, you'll need to wrap the names at 48 characters. A simple solution is to split the name on spaces, then paste the elements together again in lines of each up to 48 characters. What you have to do if a single name part exceeds 48 characters, I don't know.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think what you want is custom Comparators. You can set up a Comparator to pass a difference from its compare() method if the first name differs; if the first names are the same, then return a difference if the surnames differ. And another Comparator which starts by comparing surnames, comparing first names later.
You can embody them in your Name classYou will have to work out the logic you require for people who do not have a middle name, in which case your middleName field would be null, or people with multiple middle names. You can have a MiddleName class which has its own Comparator, but it must handle cases where the middle name is null. Also have a look in the String class, where you find a Comparator<String> ready made. Look what that does, and consider whether you need to use it in addition to the above.

I would suggest you create a simplified Name class which has two fields only, get the Comparators working on that, and later enhance them by adding middle names.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!