This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Sorting of object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Sorting of object" Watch "Sorting of object" New topic
Author

Sorting of object

Preeti Arora
Ranch Hand

Joined: Jan 26, 2007
Posts: 74
Hi,

I have fired a query for e.g

select name,des from temp;

and made a java object with setter and getter methods for name and des to store the query results.
Then I have stored all java objects in a Hashmap and sending this hashmap to jsp from servlet.
Now at jsp I am displaying this hashmap in table format.I want the results to be sorted according to say like "desc".
How can I achieve this in java?I dont want to modify query.
Thanks.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Preeti,
I don't follow why you are using a HashMap. The value is the objects you built and the key is what?

In any case, you have two choices for sorting:
1) Add a compare to method and have your object implement comparable. Then call Collections.sort() on the values in the HashMap.
2) Write your own comparator and pass it to Collections.sort


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Preeti Arora
Ranch Hand

Joined: Jan 26, 2007
Posts: 74
If i dont put it in Hashmap how will I pass it to jsp?
I am generating key of hashmap myself and adding object to it.
What is the correct way to do it?
Any code snippet?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Originally posted by Preeti Arora:
I dont want to modify query.


I see this posted a lot and it continues to astound me! Why not? The database is by far the best place to sort the results.

If i dont put it in Hashmap how will I pass it to jsp?


What makes you think that a HashMap is the only thing that you can pass to a JSP? You can pass any Java object (though it's a really bad idea to pass something like a result set directly).

A HashMap is completely unsuitable for your purpose as the order of its elements is nondeterminsitic.

My advice? Do the sorting in the DB and store the results in an array or List implementation (or other structure that guarantees order).
[ April 20, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

I agree that the database is the best place to do the sort. I recognize that some places have constraints where the query is used for other things that require different ordering though.

Bear hit on what I meant with the HashMap. It was more of a why are you using HashMap vs ArrayList question.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sorting of object
 
Similar Threads
JDBC result set - best way to store results?
ways to store results from query?
I can set the max results on a query, but can I find what the total would have been without (Paging)
Passing HashMap object from one jsp to another
Problem with ContainsKey(obj key) in HashMap