View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Harald Staff[_2_] Harald Staff[_2_] is offline
external usenet poster
 
Posts: 449
Default how to sort a string?

Hi CG

Sub test()
MsgBox SortString("bv-8,ok-3,bv-5,sk-1,bh-2,ok-9", ",")
End Sub

Function SortString(S As String, Separator As String) As String
Dim X() As String
Dim tmp As String
Dim i As Long, j As Long
'split:
X = Split(S, ",")
'trim:
For i = LBound(X) To UBound(X)
X(i) = Trim$(X(i))
Next
'sort:
For i = LBound(X) To UBound(X) - 1
For j = LBound(X) To UBound(X) - 1
If X(j) X(j + 1) Then
tmp = X(j + 1)
X(j + 1) = X(j)
X(j) = tmp
End If
Next
Next
'rebuild:
tmp = ""
For i = LBound(X) To UBound(X)
tmp = tmp & X(i) & ","
Next
tmp = Left(tmp, Len(tmp) - 1)
SortString = tmp
End Function

HTH. Best wishes Harald

"CG Rosen" wrote in message
...
Hi Group

Trying to sort a string looking like:

bv-8,ok-3,bv-5,sk-1,bh-2,ok-9 etc

into:

bh-2,bv-5,bv-8,ok-3,ok-9,sk-1 etc

Grateful for some help.

Brgds

CG Rosen