Click to See Complete Forum and Search --> : Sort a string


FinnK
June 14th, 2001, 05:21 AM
Hi all

I need a function that can sort a string:

after
str=SortStr("gduekfhe")
str should hold "deefghku"

mvh
FinnK

Surrendermonkey
June 14th, 2001, 07:24 AM
Try this



Function OrderThis(init)
Dim str(), tmpstr, vtmpstr
ReDim str(0)
ReDim tmp(0)
tmpstr = init
While tmpstr <> ""
ReDim Preserve str(UBound(str) + 1)
vtmpstr = Left(tmpstr, 1)
tmpstr = Mid(tmpstr, 2)
str(UBound(str)) = vtmpstr
Wend

Dim team1(), team2()
ReDim team1(UBound(str))
ReDim team2(UBound(str))
Dim gap, doneflag
Dim index, swap1, swap2

gap = Int(UBound(str) / 2)
Do While gap >= 1
Do
doneflag = 1
For index = 1 To UBound(str) - gap
If UCase(str(index)) > UCase(str(index + gap)) Then
swap1 = str(index)
swap2 = team1(index)
str(index) = str(index + gap)
team1(index) = team1(index + gap)
str(index + gap) = swap1
team1(index + gap) = swap2
doneflag = 0
End If
Next
Loop Until doneflag = 1
gap = Int(gap / 2)
Loop
For index = 1 To UBound(str)
OrderThis = OrderThis & str(index)
Next
End Function


Colon-Hyphen-Close-Bracket