by Paul Crowdy (4 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (6 Votes)
A very simpile example of sorting. Not the most efficeint, but easy for beginners to see whats happening.
Inputs
Unsorted Array, Sort order (True = Sort Ascending)
Code Returns
Sorted Array
Private Sub Form_Load()
Dim a(10)
a(0) = 2
a(1) = 5
a(2) = 7
a(3) = 6
a(4) = 13
a(5) = "b"
a(6) = 65
a(7) = 0
a(8) = 4
a(9) = "a"
a(10) = 1000
Sort a, False
For i = 0 To 10
Debug.Print a(i)
Next i
End Sub
Sub Sort(ByRef Arr() As Variant, Optional ByVal bAsc As Boolean = True)
Dim Done As Boolean
Done = False
Do While Done = False
Done = True
For i = 0 To UBound(Arr) - 1
If (Arr(i) > Arr(i + 1) And bAsc) Or (Arr(i) < Arr(i + 1) And Not bAsc) Then Swap Arr(i), Arr(i + 1): Done = False
Next i
Loop
End Sub
Sub Swap(ByRef a As Variant, ByRef b As Variant)
Dim tmp As Variant
tmp = a
a = b
b = tmp
End Sub