I need to sort an arraylist that contains a list of individual object. Each individual object contains an arraylist of addresses. Could someone suggest what design pattern is suitable to perform the sorting?
Kyle, Unfortunately, this is what I am doing. Since I don't know the list size until runtime, I use nested vectors. The outer one contains a list of individual (last name, first name, date of birth,...), and the nested one contains a list of addresses(current address, previous address, ...). You indicated my code is smell. Could you give me some suggestions or point me to the document where I can clean it up.
For the sorting, I had a class that implements comparator and used Collections.sort(List, Comparator) method to perform the sort. My pseudo code for the compare() method 1. Compare last name, first name, date of birth,... for the outer vector. 2. If decision cannot be made (they are all equals) then a. Sort the inner lists (the address vectors) b. Loop through the list and compare street num, street name, city, ...
What I am asking is the design pattern. Do you know any design pattern I can apply? Thanks in advance.
Joined: Aug 10, 2001
Joey, your question isn't quite clear to me. Which are you sorting? The list of individuals, the list of addresses of each individual or both?
Joined: Apr 20, 2001
Kyle, It is both. I have a master list that contains many individuals. Each individual, in turn, can contain zero or more addresses. I would like to sort the master list. Master List 1------>* Individual 1------>* addresses
Individual contains last name, first name, middle name, gender, date of birth and a list of addresses. Address contains street number, street name, apartment, city, state ,zip
At runtime, a user can specify the fields to be sorted and the sort order.
Say a user wants to sort the master list in this order: last name, first name, city, and state.
If last name and first name of two individual objects are different, compareTo() method will return either negative or positive number. If they happen to be the same, then city and state in the address objects are compared.
If I understand correctly, you really only need to sort the outer list of individuals; the fact that each individual may have multiple addresses might not really matter.
In this case, I would have the class used for the individuals implement methods like get_primary_city(), get_primary_state(). Then when you write your comparator, it first compares the names of the individuals, and if those are the same, it compares primary city or state, &c.
Alternatively, your comparator can first compare the names of the individuals, then compare the address arrays of the individuals using whatever method you choose (e.g., whoever has more addresses goes first or whatever, if tied, use information from the first address, then the second, or whatever).
Joined: Apr 20, 2001
The columns to be sorted are known at runtime. They can be just columns in individual object, columns in both individual object and address objects, or just columns in address object. If they are just city and state, the sort will be performed just on the nested object (address object). I am searching for a design pattern to solve the puzzle.