ASP Code

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

%>
Share

6 Comments

  1. 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

  2. That doesn't look like a multidimensional array, you should be able to sort it like a normal array

  3. 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.

  4. 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

  5. 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

  6. 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?

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image