by BUBA_KiNG (6 Submissions)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 19th March 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Sorts an array of strings
Function strSort(arrToSort As Variant) As Variant
Dim strTemp As String
For y = LBound(arrToSort) To UBound(arrToSort)
For x = LBound(arrToSort) To UBound(arrToSort)
For N = 1 To Len(arrToSort(y))
If Asc(Mid(arrToSort(y), N, 1)) > Asc(Mid(arrToSort(x), N, 1)) Then
GoTo Step2
ElseIf Asc(Mid(arrToSort(y), N, 1)) = Asc(Mid(arrToSort(x), N, 1)) Then
GoTo Step3
Else
strTemp = arrToSort(x)
arrToSort(x) = arrToSort(y)
arrToSort(y) = strTemp
Exit For
End If
Step3:
Next N
Step2:
Next x
Next y
strSort = arrToSort
End Function
'End Of function
'Start ex.
'here follows a way of using the sorter.
'expects: "List1" (list control) and "Command1" (button)
Private Sub Command1_Click()
arr = strSort(DummyArr(30)) 'sends a dummy array to be sorted
For i = 0 To UBound(arr)
List1.AddItem arr(i) 'desplays te result
Next
End Sub
'create a dummy array to use!
Function DummyArr(N As Integer) As Variant
Dim arr() As String
ReDim arr(0 To N) As String
Dim rndInt As Integer
Dim finStr As String
Const abc = "ABCDEFGHIJKLMOPQRST"
For i = 0 To N
For x = 1 To 10
Randomize
rndInt = Int((Rnd * Len(abc)) + 1)
finStr = finStr & Mid(abc, rndInt, 1)
Next
arr(i) = finStr
finStr = ""
Next
DummyArr = arr
End Function
'end ex