Turn Left - A weblog by InetSolution

how to sort an array in ASP

By Eric Repec
Posted on Jun 27, 2006

Comments (1)

When working with database recordsets in an Active Server Pages environment, I often need to store my data in an array. The difficulty with arrays is that there are no native functions in VB Script to easily sort an array. I've created a quick function that will easily sort arrays.

This function will reorder all the information in each row of the array so that the selected array column is sorted. The first row of the array is used to decide which variable types are being used for the sort column. I use this in many of my sites where I have to pull back encrypted data from the database. I decrypt the data with our MemberProtect object inside of the ASP script then place all the data into an array.

Once the data is in the array I can sort it and display it to the user. Future enhancements to this function will include the ability to select which column of the array you would like to sort on.

' sort a multi dem array
' modified to allow different cols to be the key for the sort.
' modified to sort dates properly
' modified to allow you to specify a sort direction 'a' or 'd'
' Fixed issue with not finding a column with data in it to identify the type.
' Fixed issue when tring to sort numbers.
' Fixed issue when sometimes it didn't compare the number properly. Casted to cdbl
' Useage arraysort(array to be sorted, column to sort on, direction to sort (a|d)
' By: Eric Repec
' InetSolution Inc
' http://www.inetsolution.com
function arraysort(values(),intSortCol,strDirection)
Dim i
Dim j
Dim value
Dim value_j
dim min
dim max
dim temp
dim datatype
dim intComp
dim intA
dim intCheckIndex

min = lbound(values,2)
max = ubound(values,2)

' check to see what direction you want to sort.
if lcase(strDirection) = "d" then
intComp = -1
else
intComp = 1
end if

if intSortCol < 0 or intSortCol > ubound(values,1) then
arraysort = values
exit function
end if
' find the first item which has valid data in it to sort
intCheckIndex = min
while len(trim(values(intSortCol,intCheckIndex))) = 0 and intCheckIndex < ubound(values,2)
intCheckIndex = intCheckIndex + 1
wend
if isDate(trim(values(intSortCol,intCheckIndex))) then
datatype = 1
else
if isNumeric(trim(values(intSortCol,intCheckIndex))) then
datatype = 2
else
datatype = 0
end if
end if
For i = min To max - 1
value = values(intSortCol,i)
value_j = i
For j = i + 1 To max
select case datatype
case 0
' See if values(j) is smaller. works with strings now.
If strComp(values(intSortCol,j),value,vbTextCompare) = intComp Then
' Save the new smallest value.
value = values(intSortCol,j)
value_j = j
End If
case 1
if intComp = -1 then
if DateDiff("s",values(intSortCol,j),value) > 0 then
' Save the new smallest value.
value = values(intSortCol,j)
value_j = j
end if
else
if DateDiff("s",values(intSortCol,j),value) < 0 then
' Save the new smallest value.
value = values(intSortCol,j)
value_j = j
end if
end if
case 2
if intComp = -1 then
if cdbl(values(intSortCol,j)) < cdbl(value) then
' Save the new smallest value.
value = values(intSortCol,j)
value_j = j
end if
else
if cdbl(values(intSortCol,j)) > cdbl(value) then
' Save the new smallest value.
value = values(intSortCol,j)
value_j = j
end if
end if
end select
Next 'j
If value_j <> i Then
' Swap items i and value_j.
for intA = 0 to ubound(values,1)
temp = values(intA,value_j)
values(intA,value_j) = values(intA,i)
values(intA,i) = temp
next 'intA
End If
Next 'i
arraysort = values
End function

Comments

I believe that in date comparison there is a little bug, " > 0 " and "< 0 " should be inverted, otherwise the descending order will be ascending and vice versa.

By marcus
Posted on Jul 7, 2007

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Who is InetSolution?

Donovan - Creative Director
Justin - Lead Architect & Developer
Somer - Graphic Designer
Mac - Programmer
Larry - Programmer
Mosh - Programmer
Paul - Technical Sales Architect
Jay - Weekend & Holidays Sys Admin
Karen - Business Development & Client Care
Jason - Project Director

Our Services

Web Design/Development
We practice a user-centered development philosophy. We work with clients who place their customer's needs first. We need to know who will use your site and why.
Secure File Exchange
Turn your website into a state-of-the-art file exchange system, requiring only a web browser, username and a password.
eCommerce Web Development
We have experience, know-how and superior customer support to ensure that your store is profitable and that your investment with InetSolution earns a high return.
Disaster Recovery Hosting
We provide fully-managed SQL server database hosting for companies seeking a warm disaster recovery site.

Category Archives

About InetSolution

We make business websites profitable. We do it with usable design, solid programming and unique, methodical marketing.