• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Design pattern for sorting nested arraylists

 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ping...
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic