How can do sorting things like that?
I am not sure where I should post it, but I am doing it with JSP now.
Let's say:
I use the getResult.jsp to retrieve some personal info stored in the database. It includes:
Name, Money_he_have, date_of_birth, Address.
A, 1000,5-10-1997, NY
B, 500,3-4-1994, NJ
D,1500,5-4-1999, CT
I wantthe user can re-sequence it, such as by Name, or by money, by date. The user can just click the list at the first line ( like click on "Name") and the result will re-order by the sequence of this column that the user clicked. If he click the "Name" again. I will reverse the sequence it list before.
I have no idea how to do that. I don't think I need every time the user click the link, I need retrieve data directly from database again. I think maybe JS can do it but I have no clue now. If any one can give me some examples
or some link I can check, it would be great.
Thanx.
[942 byte] By [
aidshiv98] at [2007-9-26 3:17:56]

Javascript can sort arrays, i.e. you can do
var x = new Array();
x.sort();
and that will sort an array of strings by there string value. There are other ways of sorting objects as well, but I don't think this is what you want.
If you wanted to use DOM/DHTML features you could resort the information by appending and removing nodes in you HTML, but that would limit who could view your site (only those with DOM1 API's) .
You could also load all the information into a Bean and then run sort function calls to that Bean.
Your best bet is the slow get the information from the database again routine. Just add an ORDER BY to your SQL for each column.
Hope that Helps,
Bryan
Definitely its not efficient way to fire a DB call every time uses click on the title...
U can try some thing like below...
Define an object (like a bean)
public Class DataObject {
String name;
Double price;
Date date;
etc ....
}
Now define a Array which can accommodate the above objects.
DataObject[] resultArray = new DataObject[length of resultset]
From your resultset for ecah row create a DataObject and store in the array.
Write custom method
sortResultArray(String sortAttribute, int order)
{
// Your code here to sort array depending on parameters
}
You should be calling this method depending on what user clicks ( name money etc )
Hope this helps you
Thanks
Sampath ..
The best method, in my humble opinion, would be to
1. store those objects (Name, Money_he_have, date_of_birth, Address) in a List data structure such as a Vector, LinkedLIst, etc. ( http://java.sun.com/products/jdk/1.2/docs/api/java/util/List.html ), and keep that data structure in memory, maybe in the session if the UI is web-based.
2. Then, you could use the Comparator interface ( http://java.sun.com/products/jdk/1.2/docs/api/java/util/Comparator.html ) to order a List of objects based on a given member variable of those objects, such as Name, date_of_birth, etc.
If you can't figure it out from the javadocs, I have some samples I could probably send you, just email me.
-Derek
Thanks everyone. It really help.
But maybe I still need a little more code. The other question as I mention in the post. How can the same link
reverse the order:
Just click the "name" first: it will became: A, B, C, D.
If you click the "Name" again, it should be: D, C, B, A.
If you click again: it will back to A, B, C, D.
How can Iadd some flag then to record how many time the user clicked?
if you look on my method signature we are passing a parameter int order - > this would help u to sort either Ascending/Desending
From you HTML link U should be calling a servlet with parameters ( name, order ) inturn these are passed to method sort(name,order)
You have to keep track of order with a hidden variable for each coloum headding ,,,
Like changing it to 0/1 for Ascending/Desending
Hope this helps you
Thanks
Sampath ..
Hi There,Could you please give me some sample code for sorting the html/table by clicking on the column heading. Could you please post the code on the same topic that being discussed. Your help is greatly appreciated. Thanks again.
mpaprp at 2007-6-29 11:31:33 >

I think the best say to do the sorting is use XML and XSLT.
> The best method, in my humble opinion, would be to
>
> 1. store those objects (Name, Money_he_have,
> date_of_birth, Address) in a List data structure such
> as a Vector, LinkedLIst, etc. (
> http://java.sun.com/products/jdk/1.2/docs/api/java/util
> List.html ), and keep that data structure in memory,
> maybe in the session if the UI is web-based.
>
> 2. Then, you could use the Comparator interface (
> http://java.sun.com/products/jdk/1.2/docs/api/java/util
> Comparator.html ) to order a List of objects based on
> a given member variable of those objects, such as
> Name, date_of_birth, etc.
>
> If you can't figure it out from the javadocs, I have
> some samples I could probably send you, just email
> me.
>
> -Derek
Hi derek, i agree about that collection framework+comparator, but if we put this collection in session scope, how do we know when that object is not in use anymore, i mean so we can delete it from session?
this is because maybe we have to create this collection object for each request, and at the end, if we dont delete it, wouldn't it cause some kind of "memory leak" ?
This can be done using a custom tag: http://www.dotjonline.com/taglib/grid.jsp
jpardi at 2007-6-29 11:31:33 >
