This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Sorting of Java objects

 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Wondering what peoples opinions are about the best way to sort a list of Java objects. I will have an HTML table (on a page of a web app) of data where the data is sorted within groups. I will populate the data with a list of java objects. For example, I may have a table that is sorted by last name, and grouped by level as follows :


Name Level Age
Adams 1 29
Woods 1 26
Butler 2 21
Thomas 2 28

If the user clicks on Age, then the table must be sorted by age, but retain the grouping by level, as follows:

Name Level Age
Woods 1 26
Adams 1 29
Butler 2 21
Thomas 2 28

My question is this. Is it better to store the data in an object in the session, and sort it on server side using comparators when the user clicks on a table column heading, or is it better to just hit the database again, and let sql do the sorting. Because this is not just straight sorting, but sorting within groups, I am leaning towards just hitting the database with new sql that contains the requested sort and group info. Does this sound like a good idea? There should be less than 100 items per list, so I dont think it will be too expensive to hit the database each time.

Thanks for any opinions,
Kim
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mi first options will be:

1. Do the sort with JavaScript
2. Do it with a scriplet
3. Use logic in your servlet (round trip to server)
4. Use a DB query (roun trio to server and SQL conection)

You are choosing the most expensive solution, I know there are only 100 rows, so it's your choice.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I second the suggestion to use JavaScript. For a maximum of 100 items it should be fast enough. See http://www.brainjar.com/dhtml/tablesort/demo.html for a demo and the tutorial at http://www.brainjar.com/dhtml/tablesort/
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Holy Sheep! That brainjar.com site is awesome. Thank you so much for the link. I will definately do this using javascript. I had done a lot of googling but didn't find anything as clearly explained as the brainjar example.

Thank you so much for your replies, this is a big help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic