"Henry Wiz" wrote:
I wanna ask how to do the following sorting function,
for example
Name Age
Brian 22
Alice 17
Patrick 21
when click on name, it will sort by
Alice
Brain
Patrick
when click on age, it will sort by
17
21
22
You can either let SQL sort for you or sort it yourself in ASP. Based on
your subject line, I'll show you one way to do it in ASP. My example is in
JScript.
Assuming you have executed a SQL query and received a recordset RS, you can
proceed as follows:
var People = new Array(),
SortBy = Request.QueryString("SortBy").Item/1 || 0
while (!RS.EOF) {
People.push([RS.Fields("Name").Value,RS.Fields("Age").Value])
RS.MoveNext()
}
People.sort(function(a,b){return a[SortBy]<b[SortBy]?1:-1})
At this point, the array [People] is sorted. Your corresponding column
headers would look something like this:
<TD><A HREF="MyScript.asp">Name</A></TD>
<TD><A HREF="MyScript.asp?SortBy=1">Age</A></TD>
You can throw a multiplier into the sort function if you want to add the
ability to sort in reverse.
--
Dave Anderson
Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.