# Thread: Simple word sorting function

1. Registered User
Join Date
Sep 2003
Location
London, England
Posts
52

## Unanswered: Simple word sorting function

I would like a function as a module that outputs the letters of a word in alphabetical order. Thus if the input is ALGORITHM then the output is f(ALGORITHM) = AGHILMORT. I am sure this should be easy to write but I am a total beginner.

2. Cavalier King Charles
Join Date
Dec 2002
Location
Préverenges, Switzerland
Posts
3,740
i hope you are not inflicting this stuff on your students!

context?
doing it ONCE is a simple VBA exercise.
if you have to do this a hundred thousand times the simple approach will be slooooooooow and the first priority would be to NOT do it if at all possible ...which takes us back to context.

izy

3. Registered User
Join Date
Sep 2003
Location
London, England
Posts
52

## Dictionary

Hello Izy
Not for my students but for my own amusement. I would like to use it many times but I would also be interested in a slow approach. Can you help me once again?

Join Date
Nov 2004
Location
out on a limb
Posts
13,692
fwiw....

Code:
```Public Function FindLetters(MyWord As String) As String
If Len(MyWord) <= 1 Then
FindLetters = MyWord 'return what we were supplied if the length is less than or equal to 1
Else
Findletters =""
Dim iLoop As Integer
'ascii codes for letters are
'65-90 = A to Z
'97-122 = a to z
For iLoop = 65 To 90 'search through for each ascii character number (both upper and lower case to find what characters are in MyWord
'if found append to the return value
FindLetters = FindLetters & FindLetter(MyWord, iLoop)
'reapeat for the lowercase of the letter
FindLetters = FindLetters & FindLetter(MyWord, iLoop + 32)
Next iLoop
End If
End Function
Private Function FindLetter(MyWord As String, LetterID As Integer) As String
FindLetter = "" 'not needed but its good practise (to me) to initialise variables
Dim StartPos As Integer 'indicates where the instr should start
'Dim MyWordLength As Integer
'MyWordLength = Len(MyWord) ' find the length of the expression we are searching
StartPos = 0
Do
StartPos = InStr(StartPos + 1, MyWord, Chr\$(LetterID), vbBinaryCompare)
If StartPos > 0 Then 'we have found the character
'Don't mention to Missinglinq that we are usign the \$ function.......
FindLetter = FindLetter & Chr\$(LetterID)  'add the letter
End If
Loop While StartPos <> 0
End Function```
Last edited by healdem; 12-21-08 at 15:27.

5. Registered User
Join Date
Sep 2003
Location
London, England
Posts
52

## Thank you

It seems it was more difficult than I had thought. Thank you very much, it's just what I wanted.

6. www.gvee.co.uk
Join Date
Jan 2007
Location
UK
Posts
11,445
I did something like this a while back, it's essentially a bubble sort algorithm.

7. Registered User
Join Date
Sep 2003
Location
London, England
Posts
52
Thanks, Georgev, I understand that the Bubble Sort algorithm would be good here.

8. Cavalier King Charles
Join Date
Dec 2002
Location
Préverenges, Switzerland
Posts
3,740
and here's another route - i call it a "binary-tree" sort but i don't know the official name. it scales much better than most other sorts.

izy

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•