Geek Stuff > Tutorials > Sorting a Two-Dimensional Array in Visual Basic

Sorting a Two-Dimensional Array in Visual Basic



As a programmer, you will eventually reach the point where you need to sort a two-dimensional array by column while maintaining the integrity of the array. Recently, while trying to write a simple database program intended to parse and sort CSV files, I reached this dilemma. Most forums and support articles said that sorting a 2D array was impossible and should not be attempted.

Not willing to take no for an answer, I developed a simple method in Visual Basic. Even though it's pretty clunky and probably uses way more memory than it should, it gets the job done in a reasonable amount of time.

The method

The implementation of the method can be broken down into a few steps:

  1. Create two storage arrays, one to store keys and the original indices of those keys
  2. Pass these arrays to the Array.Sort method, which sorts both arrays to keep the keys even with their indices
  3. Create a two-dimensional list and use this to sort the two-dimensional array based on keys
  4. Convert the two-dimensional list to a two-dimensional array and return it

This method can be implemented as follows: (Note: this code was written freehand hasn't been tested yet. It'll be tested soon.)

' Function Sort: Sorts a two-dimensional array by column
' Returns the sorted two-dimensional array
' Params:
'     arrToSort- 	The two-dimensional array to sort
'     columnToSortBy-	The zero-based position of the column to sort by
'     opt startSortAt-	The zero-based position to sort sorting at
Function Sort(ByVal arrToSort()() as Object, ByVal columnToSortBy as Integer, Optional ByVal startSortAt as Integer = 0)
	' Check to see if array is zero length
	If arrToSort.Length = 0 Then Return arrToSort
	' Create storage array for keys
	Dim tempKeys as New List(Of Object)
	For i = startSortAt To arrToSort.Length - 1
	Dim keys as Object() = tempKeys.ToArray

	' Create storage array for indices '
	Dim tempIndices as New List(Of Integer)
	For i = startSortAt to arrToSort.Length - 1
	Dim indices as Integer() = tempIndices.ToArray

	' Sort the storage arrays '
	Array.Sort(keys, indices)

	' Create temporary two-dimensional list '
	Dim tempArrToSort as New List(Of List(Of Object))

	' Check if sort started at index other than 0 '
	' If it did, add the non-sorted entries first '
	If Not startSortAt = 0 Then
		For i = 0 to startSortAt
	End If

	' Add sorted items '
	For i = 0 to keys.Length - 1

	' Return sorted two-dimensional array '
	Return tempArrToSort.ToArray
End Function

Using this method is also rather simple. Take, for example, a database contained in memory in a two-dimensional array. The database is contained in db()() and contains these columns:

  1. Customer
  2. Acct #
  3. Charges
  4. Paid
  5. Balance
  6. Other Info

Let's say that you wanted to sort db()() by column 1, Acct #. To do this, you would use the method like this:

db = Sort(db, 1)

Rather simple. But what if the database contains 1,000,000 entries and entries 0-899999 are already sorted by the customer's name? Then you would call the method like this:

db = Sort(db, 0, 900000)

Questions? Comments? Click here to drop me a line. I'm open to criticism and happy to help you troubleshoot.