Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Spaces to Alphabetized String 2

Status
Not open for further replies.

GSCaupling

Technical User
Sep 5, 2008
296
US
I found code for a function that will alphabetize the characters in a string.

If A2 is "TACO" then Sortletters(A2) returns "ACOT." What I'd like the result to look like is "A C O T" with a space in between the characters. A leading space before the first character is okay - I'm just looking for readability.

Here is the code:

Function SortLetters(v As Variant) As String
Dim bFlag As Boolean
Dim n As Long, i As Long
Dim s As String

n = Len(v)
If n = 0 Then Exit Function

ReDim arrS(0 To Len(v))
For i = 1 To Len(v)
arrS(i) = Mid$(v, i, 1)
Next

Do
bFlag = True
For i = LBound(arrS) To UBound(arrS) - 1
If arrS(i) > arrS(i + 1) Then
bFlag = False
arrS(0) = arrS(i)
arrS(i) = arrS(i + 1)
arrS(i + 1) = arrS(0)
End If
Next i
Loop While Not bFlag

s = ““
For i = 1 To UBound(arrS)
s = s & arrS(i)
Next

SortLetters = s

End Function

What would I add to get the spacing I want?

Thanks,

GS


[Green]******^*******
[small]I[/small] [small]Hate[/small] [♥] [small]Ambiguity.[/small][/green]
 
HI,

Code:
For i = 1 To UBound(arrS)
s = s & arrS(i) & " "
Next
s = Left(s, Len(s) - 1)

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 

Awesomesauce! That works - thank you!



[Green]******^*******
[small]I[/small] [small]Hate[/small] [♥] [small]Ambiguity.[/small][/green]
 
...or you could just do:

Code:
s = Join(arrS, " ")
s = Left(s, Len(s) - 1)

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Andy, never used Join() before. Thx!

Actually you only need the one statement...
Code:
s = Join(arrS, " ")


Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
never used Join() before" you could have knocked me over with a feather :)
I 'll be walking the whole day with my nose up - I knew something Skip did not know (that's the first [wink] )

Once I've discovered [tt]Split()[/tt] and [tt]Join()[/tt] functions, I don't know how I lived without them before...

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Ah, the mystique of a persona. Having occupied nearly three quarters of a century, I still have an awful lot to learn!

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Just for fun, here's an version that leverages the .NET core library:

Code:
[blue]Public Function SortLetters(v As String) As String
    Dim char As Variant
    Dim mystring() As Byte

    mystring = StrConv(v, vbFromUnicode)
    With CreateObject("system.collections.arraylist")
        For Each char In mystring
            .Add Chr$(char)
        Next
        .Sort
        SortLetters = Join(.toarray(), " ")
    End With
End Function[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top