Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting of Java objects

 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • 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
 
Oscar Gonzalez
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic