• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting a table of ArrayLists  RSS feed

 
Richard Elsberry
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am using a jsp page to display a table that contains one to many rows and always four columns. The data is being returned from a service and each data object is a String. I need to be able to allow the user to sort the table based on any of the four columns. For example if they select one of four options from a drop down then I could sort the respective column accordingly. Normally you could handle this easily with an ORDER BY added to the query but I am not able to change the query.
To get the data, I am parsing an xml document and creating multiple ArrayLists that represent each row and putting them into another ArrayList to represent the table.
I can use Collections.sort on each ArrayList but I can't figure out a way to rearrange the colums as an entire table. Is there a simple solution that I am overlooking?
This must be a common issue. I should add that I don't have to use ArrayLists. Any approach that can maintain some sort of table structure would work.
Any thoughts or direction would be greatly appreciated.
Richard
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would write a java.util.Comparator implementation that would do the dort for you. When the user clicks on a column header, that sends a request to the sort servlet, passing a parameter indicating which column was clicked. You could write the comperator as being configurable as to which column it is sorting (my preferred way), or you could write four separate Comparator classes for each column. The sorted list is then stored in the session; you return to the JSP, obtain the list from the session, and, voila, your table is sorted!
This is big on general architecture and short on details, but you can give it a try and see how far you make it....
 
Richard Elsberry
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joel,
The part I can't seem to figure out is how to sort one row and keep the data consistent across the entire row. For example if a row contains a first name, last name, id, and password. When you sort the first name column how can you keep the other columns lined up so that last name, etc are correct?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have your Comparator sort ArrayLists, not the elements of the array list. Thus:

Then, you sort the master ArrayList (the one representing the rows), not the individual "column" array lists.
 
Richard Elsberry
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joel. This will work for me.
Also, my late reply is inexcusable. Sorry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!