<%
Dim arrDualArray(9,1), i
arrDualArray(0,0) = "Darren"
arrDualArray(0,1) = 10
arrDualArray(1,0) = "Andrew"
arrDualArray(1,1) = 5
arrDualArray(2,0) = "Paul"
arrDualArray(2,1) = 8
arrDualArray(3,0) = "Homer"
arrDualArray(3,1) = 2
arrDualArray(4,0) = "Bart"
arrDualArray(4,1) = 1
arrDualArray(5,0) = "Lisa"
arrDualArray(5,1) = 7
arrDualArray(6,0) = "Barney"
arrDualArray(6,1) = 6
arrDualArray(7,0) = "Fred"
arrDualArray(7,1) = 9
arrDualArray(8,0) = "Wilma"
arrDualArray(8,1) = 3
arrDualArray(9,0) = "Betty"
arrDualArray(9,1) = 4
Response.Write "<b><u>Unsorted List of 2d Array:</u></b><br>"
For i = LBound(arrDualArray) to UBound(arrDualArray)
Response.Write arrDualArray(i, 0) & " - "
Response.Write arrDualArray(i, 1) & "<BR>"
Next
Sub DualSorter( byRef arrArray, DimensionToSort )
Dim row, j, StartingKeyValue, StartingOtherValue, _
NewStartingKey, NewStartingOther, _
swap_pos, OtherDimension
Const column = 1
' Ensure that the user has picked a valid DimensionToSort
If DimensionToSort = 1 then
OtherDimension = 0
ElseIf DimensionToSort = 0 then
OtherDimension = 1
Else
'Shoot, invalid value of DimensionToSort
Response.Write "Invalid dimension for DimensionToSort: " & _
"must be value of 1 or 0."
Response.End
End If
For row = 0 To UBound( arrArray, column ) - 1
'Start outer loop.
'Take a snapshot of the first element
'in the array because if there is a
'smaller value elsewhere in the array
'we'll need to do a swap.
StartingKeyValue = arrArray ( row, DimensionToSort )
StartingOtherValue = arrArray ( row, OtherDimension )
' Default the Starting values to the First Record
NewStartingKey = arrArray ( row, DimensionToSort )
NewStartingOther = arrArray ( row, OtherDimension )
swap_pos = row
For j = row + 1 to UBound( arrArray, column )
'Start inner loop.
If arrArray ( j, DimensionToSort ) < NewStartingKey Then
'This is now the lowest number -
'remember it's position.
swap_pos = j
NewStartingKey = arrArray ( j, DimensionToSort )
NewStartingOther = arrArray ( j, OtherDimension )
End If
Next
If swap_pos <> row Then
'If we get here then we are about to do a swap
'within the array.
arrArray ( swap_pos, DimensionToSort ) = StartingKeyValue
arrArray ( swap_pos, OtherDimension ) = StartingOtherValue
arrArray ( row, DimensionToSort ) = NewStartingKey
arrArray ( row, OtherDimension ) = NewStartingOther
End If
Next
End Sub
call DualSorter(arrDualArray, 1)
Response.Write "<p><b><u>Sorted List of 2d Array by Number:</u></b><br>"
For i = LBound(arrDualArray) to UBound(arrDualArray)
Response.Write arrDualArray(i, 0) & " - "
Response.Write arrDualArray(i, 1) & "<BR>"
Next
call DualSorter(arrDualArray, 0)
Response.Write "<p><b><u>Sorted List of 2d Array by Name:</u></b><br>"
For i = LBound(arrDualArray) to UBound(arrDualArray)
Response.Write arrDualArray(i, 0) & " - "
Response.Write arrDualArray(i, 1) & "<BR>"
Next
%>
|