by Eric A. Johnson (4 Submissions)
Category: String Manipulation
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Sun 2nd April 2006
Date Added: Mon 8th February 2021
Rating:
(1 Votes)
This code will alphabetize any string inserted. For best results, remove any spaces. This code assumes that the string is in one case;
Private Function Alphabetize(ByVal originalString As String) As String
Dim alphabetizedString As String = ""
Dim tempString As String
Dim Iterator As Integer
Dim closestToA As Char
Dim positionToRemove As Integer
Dim charsToRemove As Integer
Dim tempIterator As Integer
tempString = originalString
charsToRemove = tempString.Length
' Two For loops to work through the string; first one is to ensure
' removal of one character each pass; second one loops through
' all characters in the current temporary string
For Iterator = 0 To charsToRemove - 1
' Set the character closest to a as equal to the first character
' of the temporary string
closestToA = tempString.Chars(0)
positionToRemove = 0
For tempIterator = 1 To tempString.Length - 1
If tempString.Chars(tempIterator) < closestToA Then
closestToA = tempString.Chars(tempIterator)
positionToRemove = tempIterator
End If
Next tempIterator
' Remove selected character from tempstring and append it to alphabetizedString
tempString = tempString.Remove(positionToRemove, 1)
alphabetizedString &= closestToA.ToString
Next Iterator
Return alphabetizedString
End Function ' Alphabetize
No comments have been posted about This code will alphabetize any string inserted. For best results, remove any spaces. This code assu. Why not be the first to post a comment about This code will alphabetize any string inserted. For best results, remove any spaces. This code assu.