sorting array question

1. Member
Join Date
Dec 1969
Posts
56

## sorting array question

Found a post from Bill about sorting an array:<BR><BR>&#060;% <BR>Sub Sort2DArray( ByRef ar, sortColumn ) <BR>Dim i, j, temp, col <BR>Dim rowMax = UBound(ar,2) <BR>Dim colMax = UBound(ar,1) <BR>For i = rowmax-1 To 0 <BR>For j = 0 To i <BR>If ar(sortColumn,j) &#062; ar(sortColumn,j+1) Then <BR>&#039; swap <BR>For col = 0 To colMax <BR>temp = ar(col,j) <BR>ar(col,j) = ar(col,j+1) <BR>ar(col,j+1) = temp <BR>Next <BR>End If <BR>Next &#039; next j <BR>Next &#039; next i <BR>End Sub <BR>%&#062; <BR><BR>works great, but I&#039;d like to sort ascending or descending. So what I&#039;ve done is:<BR>&#060;%<BR>sort = request.querystring("sort")<BR>arrUsers = getUsers(0) &#039;passed to function that returns user info by using getrows()<BR>&#039;if sorting by column, use the sort function to do it<BR>if sort &#060;&#062; "" then<BR>arrUsers = Sort2DArray(arrUsers,sort)<BR>end if<BR><BR>for i = 0 to ubound(arrUsers,2)<BR>sName = arrUsers(0,i)<BR>sEmail= arrUsers(1,i)<BR>etc etc<BR>Content = Content & sName & " - " & sEmail<BR>next<BR>response.write(Content)<BR>%&#06 2;<BR>this works great and sorts ascending. I know if I want to sort descending, I can change:<BR><BR>for i = 0 to ubound(arrUsers,2)<BR>to:<BR>for i = ubound(arrUsers,2) to 0 step -1<BR><BR>How can I dynamically create that "for" statement WITHOUT having to put the entire code in a bigass if statement? The code that builds the "Content" string is pretty long and I know if I just duplicated it, using an if statement around the entire thing would work, at the expense of having double the lines of code. I&#039;ve tried:<BR><BR>&#060;%<BR>sortorder = request.querystring("so") &#039;I pass a 1 to sort desc. null is the default option to sort ascending<BR>forstring1 = "0"<BR>forstring2 = "ubound(arrUsers,2)"<BR>if sortorder &#060;&#062; "" then<BR>forstring1 = "ubound(arrUsers,2)"<BR>forstring2 = "0 step -1"<BR>end if<BR>for i = forstring1 to forstring2<BR>sName = arrUsers(0,i)<BR>sEmail= arrUsers(1,i)<BR>etc etc<BR>Content = Content & sName & " - " & sEmail<BR>next<BR>response.write(Content)<BR>%&#06 2;<BR><BR>I just end up with a type mismatch. I know why it&#039;s happening, just don&#039;t know how to fix or accomplish what I want. I looked at a reaaaaaaaaaly complex vb array sort function at http://www.evolt.org/article/Quick_and_Dirty_ASP_Array_Sorting/17/21724 but it is just soooo memory intense, once my data gets huge, this method will just destroy the server.

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Easy...

this works great and sorts ascending. I know if I want to sort descending, I can change:<BR><BR> for i = 0 to ubound(arrUsers,2)<BR>to:<BR> for i = ubound(arrUsers,2) to 0 step -1<BR><BR>How can I dynamically create that "for" statement WITHOUT having to put the entire code in a bigass if statement?<BR><BR>*************<BR><BR>&#060;%<BR>If ascending Then<BR> startAt = 0<BR> endAt = UBound(arrUsers,2)<BR> stepBy = 1<BR>Else<BR> startAt = UBound(arrUsers,2)<BR> endAt = 0<BR> stepBy = -1<BR>End If<BR><BR>FOR row = startAt TO endAt STEP stepBy<BR> ...<BR>NEXT<BR>%&#062;<BR><BR>No, you can&#039;t use strings.<BR><BR>

3. Member
Join Date
Dec 1969
Posts
56

## RE: Easy...

ahhhhhhhhh, thanks Bill, knew it was something simple.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•