Our Blog

A blog by InetSolution about programming, security, design and marketing for banks, credit unions and e-commerce.

How to sort an array in classic ASP

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.


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

Other Recent Blog Posts

Find this useful?

Want to receive our monthly tip to make your website easier to use and safer? No spam, just good advice. Signup!

Interests

Blog RSS Feed

Request a Consultation

Let us help you accomplish big goals.

Help us prevent spam: