Two-Dimensional Array Sort Using Bubble Sort

This demo illustrates how to perform a sort on a two-dimensional array using Bubble Sort.


Unsorted List of 2d Array:
Darren - 10
Andrew - 5
Paul - 8
Homer - 2
Bart - 1
Lisa - 7
Barney - 6
Fred - 9
Wilma - 3
Betty - 4

Sorted List of 2d Array by Number:
(Called via: call DualSorter(arrDualArray, 1))
Bart - 1
Homer - 2
Wilma - 3
Betty - 4
Andrew - 5
Barney - 6
Lisa - 7
Paul - 8
Fred - 9
Darren - 10

Sorted List of 2d Array by Name:
(Called via: call DualSorter(arrDualArray, 0))
Andrew - 5
Barney - 6
Bart - 1
Betty - 4
Darren - 10
Fred - 9
Homer - 2
Lisa - 7
Paul - 8
Wilma - 3


Souce Code:
<%
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
%>


[Back to the article] | [View the "sorting an array of objects" demo]