To read the article online, visit http://www.4GuysFromRolla.com/webtech/011601-1.shtml

# Sorting a Two-Dimensional Array using a Bubble Sort

By Darren Neimke

Before reading this article, it is highly suggested that you read the first part of this article, Sorting a One-Dimensional Array using a Bubble Sort. This previous article discussed the intracacies of the Bubble Sort, including a performance comparison to QuickSort.

Sorting a Two-Dimensional Array with Bubble Sort
This article assumes that you have read my previous article on Bubble Sort with a one-dimensional array. In this article, we're going to jump into sorting a two-dimensional array and skip past a lot of the explanation that was presented in the first article. First of all let's create a 2-dimensional Array an write it's contents to the screen:

 ```Dim arrDualArray(9,1) 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 For i = LBound(arrDualArray) to UBound(arrDualArray) Response.Write arrDualArray(i, 0) & " - " Response.Write arrDualArray(i, 1) & "
" Next ```

That gives us an unsorted list that looks like this:

```Darren - 10
Andrew - 5
Paul - 8
Homer - 2
Bart - 1
Lisa - 7
Barney - 6
Fred - 9
Wilma - 3
Betty - 4
```

However, with some minor changes to our one dimensional sort routine we can not only sort our array but also declare with dimension to sort the array by! The code for this function is presented below, along with a live demo.

 ```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 ```
[View the live demo!]

Now we simply call our routine, passing in our array and the dimension that we wish to sort by:

 ``` call DualSorter(arrDualArray, 1) ```

Since we are creating a zero-based array, the `1` in the above function call informs the function to sort the second dimension. If we wanted to sort by the first dimension we would pass in a `0`

And return the results to the screen

 ```For i = LBound(arrDualArray) to UBound(arrDualArray) Response.Write arrDualArray(i, 0) & " - " Response.Write arrDualArray(i, 1) & "
" Next ```

Gives us the following output. As you can see - problem sorted:

```Bart - 1
Homer - 2
Wilma - 3
Betty - 4
Andrew - 5
Barney - 6
Lisa - 7
Paul - 8
Fred - 9
Darren - 10
```

Now that we've shown how to sort a two-dimensional array of scalar variables, let's look at how we can sort an array of objects! We'll examine this topic in Part 2 of this article.