Sort an array in ascending 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.
LotusScript


Sub Click(Source As Button)

	Dim TestArray(6) As String

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

	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) = "A"
TestArray(1) = "B"
TestArray(2) = "C"
TestArray(3) = "D"
TestArray(4) = "E"
TestArray(5) = "F"
TestArray(6) = "G"

Posted by fbrefere001 on Wednesday March 20, 2002