aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Design pattern for sorting nested arraylists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Design pattern for sorting nested arraylists" Watch "Design pattern for sorting nested arraylists" New topic
Author

Design pattern for sorting nested arraylists

Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
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 Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
My first recommendation is not to fill the array with lists (if that's what you're doing -- I can't quite tell). Collections of collections is a design smell that you should avoid in general.

If instead the array contained your own classes (and the classes implemented the compareTo() method) then you'd just use Collections.sort() to sort it.

However, if you can't change the data structure, you can always write a Comparator class, and use the two-argument sort() method of Collections.

See the Collections javadoc for more information.

Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
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.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
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?

Kyle
Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
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.
Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
Ping...
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
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).
Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design pattern for sorting nested arraylists