EJB design: EJB for data sorting.
Does any body know if there is a server side sorting component available in the market. The component should accept a collection of objects and a sort parameter, I can pass the collection in the form of resultset from a database and a sort field as an input to the component and the component should return the sorted list of objects(Result set in this case).
I need to do this because there is a requirement that users want to sort the table on a web page based on certain field. The use click the field and the sort request will be processed by the JSP or the servlet at the server and the results will be send back to the user. The data are cached on the Server so there is no involvement of database for sorting purpose.
Any help would be greatly appreciated. It's time critical.
As per your mail, the data is already cached on the server. If it is in a Java collection or an array, you can use the sort() method provided by the Collections class. Here is simple examples from Java tutorials - http://java.sun.com/docs/books/tutorial/collections/algorithms/index.html.
With minimal efforts you can write different comparators and pass it in to sort method to sort the collections in required order.
If the data is already with the servlet as a collection object, it might be a good idea to put the logic in the servlet.
Hope this helps.
Thanks a lot Satish for your help. I realy appreciate it. I will try the solution that you suggested. If I have any questions, will that be possible for me to contact you by email or so.
Thanks a lot
I tried to sort the arrays using collection object but I can only single dimension arrays. For a database resultset I need to sort a multi demensional array. How can I do that? Do you hav eany idea?
You can not sort the result set directly. You need to do the following -
1. Create a class having all the columns in the resultset
2. Loop thru the result set
2.1 For each row in the resultset create an object
2.2 Add the above object to the Collection
3. Create a comparator as per your sort criteria
4. Sort the collection by passing the comparator
3. Use the sorted collection
Read the collections tutorial on Javasoft.com. It is very good and it takes hardly an hour to read it.