cgmojoco
Well-known Member
- Joined
- Jan 15, 2005
- Messages
- 699
I have a string that I need to sort.
Below code works beautifully but doesn't sort numbers, any advice to get this to sort numbers as well?
Below code works beautifully but doesn't sort numbers, any advice to get this to sort numbers as well?
Code:
Function Alphabetize(ByVal sText As String) As String
Dim sWords() As String, sTemp As String
Dim i As Long, j As Long, n As Long
'-- clean up text
For i = 1 To Len(sText)
Select Case Mid$(sText, i, 1)
Case " ", "a" To "z", "A" To "Z"
Case Else: Mid$(sText, i, 1) = " "
End Select
Next
'-- remove leading and trailing spaces
sText = Trim(sText)
'-- remove multi spaces
Do While InStr(sText, " ")
sText = Replace(sText, " ", " ")
Loop
'-- split text by space
sWords = Split(sText)
n = UBound(sWords)
If n = 0 Then '-- only one word
Alphabetize = sText
Else
'-- sort array
For i = 0 To n - 1
For j = i + 1 To n
If sWords(i) > sWords(j) Then
sTemp = sWords(i)
sWords(i) = sWords(j)
sWords(j) = sTemp
End If
Next
Next
Alphabetize = Join(sWords, ",")
End If
End Function