File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Sorting of Java objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting of Java objects" Watch "Sorting of Java objects" New topic
Author

Sorting of Java objects

Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
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

Joined: Mar 28, 2006
Posts: 63
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
Marshal

Joined: Mar 22, 2005
Posts: 41838
    
  63
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/


Ping & DNS - my free Android networking tools app
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting of Java objects