This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
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 object

 
Preeti Arora
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Preeti Arora
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic