Hi this something I came up with to sort a basic array of numbers.
and I thought I share it with the rest in the hope it come in useful.
vb Code:
Private Sub Swap(a As Variant, b As Variant) Dim t As Variant 'Swap a and b t = a a = b b = t End Sub Private Sub Sort(arr As Variant) Dim X As Long, Y As Long, Size As Long On Error Resume Next 'Get array size Size = UBound(arr) 'Do the sorting. For X = 0 To Size For Y = X To Size If arr(Y) < arr(X) Then 'Swap vals Call Swap(arr(X), arr(Y)) End If Next Y Next X End Sub
Example
vb Code:
Private Sub Command1_Click() Dim Nums(0 To 5) As Variant 'Add some random numbers. Nums(0) = 4 Nums(1) = 2 Nums(2) = 6 Nums(3) = 3 Nums(4) = 5 Nums(5) = 0.1 Call Sort(Nums) 'Display sorted results. MsgBox Nums(0) MsgBox Nums(1) MsgBox Nums(2) MsgBox Nums(3) MsgBox Nums(4) MsgBox Nums(5) End Sub




Reply With Quote