Sort a multi-dimensional array in descending order

This code will sort the values of an array in alphabetical or numerical order. The test example below creates a text array of letters in random order and calls the SortArray Sub function to sort the values in descending order
LotusScript


Sub Click(Source As Button)

	Dim TestArray(6, 0) As String

	TestArray(0, 0) = "C, 3"
	TestArray(1, 0) = "F, 6"
	TestArray(2, 0) = "A, 1"
	TestArray(3, 0) = "D, 4"
	TestArray(4, 0) = "G, 7"
	TestArray(5, 0) = "B, 2"
	TestArray(6, 0) = "E, 5"

	Call SortArray(TestArray)

End Sub


Sub SortArray(array)

	n = Ubound(Array) + 1

	For X = 1 To (n - 1) 'Perform the following loop for each value in the arrays
		J = X
		Do While J >= 1
			If Array(J) > Array(J - 1) Then ' Compares two values in the array
				ValueA = Array(J) ' Swap the values compared since the second is less than the first
				ValueB = Array(J - 1)
				Array(J) = ValueB
				Array(J - 1) = ValueA
				J = J - 1 ' Increment to the next two down in the array (descending to index 0 )
			Else
				Exit Do ' Index 0 reached, goto next X index in the array and loop again
			End If
		Loop
	Next

End Sub


RESULT: 
TestArray(0,0) = "G, 7"
TestArray(1,0) = "F, 6"
TestArray(2,0) = "E, 5"
TestArray(3,0) = "D, 4"
TestArray(4,0) = "C, 3"
TestArray(5,0) = "B, 2"
TestArray(6,0) = "A, 1"

Posted by fbrefere001 on Wednesday March 20, 2002