March 14th, 2006
ASP: Sort a Multi-Dimensional Array
This function will enable you to sort a multi-dimensional array by whichever dimension you want
<%
'========================'
function arraySort( arToSort, sortBy, compareDates )
'========================'
Dim c, d, e, smallestValue, smallestIndex, tempValue
For c = 0 To uBound( arToSort, 2 ) - 1
'=== set smallest to current
smallestValue = arToSort( sortBy, c )
smallestIndex = c
For d = c + 1 To uBound( arToSort, 2 ) '=== find smallest and set
if not compareDates then
if strComp( arToSort( sortBy, d ), smallestValue ) < 0 Then
smallestValue = arToSort( sortBy, d )
smallestIndex = d
End if
else
if not isDate( smallestValue ) then '=== recurse to function comparing as strings
arraySort = arraySort( arToSort, sortBy, false )
exit function
else
if dateDiff( "d", arToSort( sortBy, d ), smallestValue ) > 0 Then
smallestValue = arToSort( sortBy, d )
smallestIndex = d
End if
end if
end if
Next
if smallestIndex <> c Then 'swap
For e = 0 To uBound( arToSort, 1 )
tempValue = arToSort( e, smallestIndex )
arToSort( e, smallestIndex ) = arToSort( e, c )
arToSort( e, c ) = tempValue
Next
End if
Next
arraySort = arToSort
end function
%>


on January 24th, 2007 at 10:39 am
Hi,
I have used your script, but came up with a problem.
I use it in a search engine in which I give a percentage of match.
But now I get the following sorting:
100
17
17
17
33
83
As you can see the function sorts only on the first digit, but I want it to sort on the whole number! Can you please help me with this?
Thanks,
Jeroen
on January 24th, 2007 at 10:47 am
That doesn't look like a multidimensional array, you should be able to sort it like a normal array
on January 29th, 2007 at 2:27 am
Sorry,
I just displayed a part of the array.
It looks like this:
array(score, title, link)
where score is a number (see previous comment) and title and link are text.
Hope you can help me.
on January 29th, 2007 at 9:02 am
You should make sure that the 'score' is a valid integer (maybe use cInt()), otherwise it may be comparing them as strings, which would account for the wonky sort results
on September 4th, 2008 at 6:51 am
why it returns error when
arTest = arraySort(arTest,1,true)
is called
code————————
function arraySort( arToSort, sortBy, compareDates )
Dim c, d, e, smallestValue, smallestIndex, tempValue
For c = 0 To uBound( arToSort, 2 ) – 1
"=== set smallest to current
smallestValue = arToSort( sortBy, c )
smallestIndex = c
For d = c + 1 To uBound( arToSort, 2 ) "=== find smallest and set
if not compareDates then
if strComp( arToSort( sortBy, d ), smallestValue ) 0 Then
smallestValue = arToSort( sortBy, d )
smallestIndex = d
End if
end if
end if
Next
if smallestIndex c Then ''swap
For e = 0 To uBound( arToSort, 1 )
tempValue = arToSort( e, smallestIndex )
arToSort( e, smallestIndex ) = arToSort( e, c )
arToSort( e, c ) = tempValue
Next
End if
Next
arraySort = arToSort
end function
dim arTest(2,2)
arTest(0, 0) = 1
arTest(0, 1) = "01-01-2004"
arTest(0, 2) = "Event #1"
arTest(1, 0) = 2
arTest(1, 1) = "10-01-2005"
arTest(1, 2) = "Event #2"
arTest(2, 0) = 3
arTest(2, 1) = "02-01-2006"
arTest(2, 2) = "Event #3"
arTest = arraySort(arTest,1,true)
For i=LBound(arTest,1) to UBound(arTest,1)
For j=LBound(arTest,2) to UBound(arTest,2)
response.write(arTest(i,j) & " ")
next
if j mod 3 = 0 then response.write(" ")
next
on April 10th, 2009 at 12:51 pm
I think I'm running into the same problem as linx. I get a "type mismatch" error when I do eventArray=arraySort(eventArray,1,true)
Any suggestions?