How do I make this sort in true alphabetical order instead of ascii order, I am
ID: 3792271 • Letter: H
Question
How do I make this sort in true alphabetical order instead of ascii order, I am using visual basics?? I have it understanding what I want, but I want it in true alphabetical order.
Public Class frmTransfer
Dim strSortArray() As String
Private Sub txtInput_TextChanged(sender As Object, e As EventArgs) Handles txtInput.TextChanged
Dim intUpper As Integer
Dim intCount As Integer
intUpper = Len(txtInput.Text) - 1 REM -1 because array starts at 0
For Each chr As Char In txtInput.Text REM for each chr(character) entered as a character in the text box
If ((Asc(chr) > 64) And (Asc(chr) < 91)) Or ((Asc(chr) > 96) And (Asc(chr) < 123)) Or ((Asc(chr) > 31) And (Asc(chr) < 65)) Then REM If the character entered is a letter
ReDim Preserve strSortArray(intUpper) REM preservers previous data and adds new slot to array
strSortArray(intCount) = chr REM sets the array at position intCount equal to chr
intCount = intCount + 1 REM increases intCount by 1
End If
Next chr
End Sub
Private Sub btnTransfer_Click(sender As Object, e As EventArgs) Handles btnTransfer.Click
Dim X As Integer
Dim Y As Integer
Dim temp As String
For X = LBound(strSortArray) To (UBound(strSortArray) - 1)
For Y = LBound(strSortArray) To (UBound(strSortArray) - 1)
If Asc(strSortArray(Y)) > Asc(strSortArray(Y + 1)) Then
' exchange the items
temp = strSortArray(Y)
strSortArray(Y) = strSortArray(Y + 1)
strSortArray(Y + 1) = temp
End If
Next Y
Next X
lblOutput.Text = String.Join("", strSortArray)
End Sub
End Class
Explanation / Answer
Public Class frmTransfer
Dim strSortArray() As String
Private Sub txtInput_TextChanged(sender As Object, e As EventArgs) Handles txtInput.TextChanged
Dim intUpper As Integer
Dim intCount As Integer
intUpper = Len(txtInput.Text) - 1 REM -1 because array starts at 0
For Each chr As Char In txtInput.Text REM for each chr(character) entered as a character in the text box
If ((Asc(chr) > 64) And (Asc(chr) < 91)) Or ((Asc(chr) > 96) And (Asc(chr) < 123)) Or ((Asc(chr) > 31) And (Asc(chr) < 65)) Then REM If the character entered is a letter
ReDim Preserve strSortArray(intUpper) REM preservers previous data and adds new slot to array
strSortArray(intCount) = chr REM sets the array at position intCount equal to chr
intCount = intCount + 1 REM increases intCount by 1
End If
Next chr
End Sub
Private Sub btnTransfer_Click(sender As Object, e As EventArgs) Handles btnTransfer.Click
{
{
Dim X As Integer
Dim Y As Integer
Dim temp As String
For X = LBound(strSortArray) To (UBound(strSortArray) - 1)
For Y = LBound(strSortArray) To (UBound(strSortArray) - 1)
If Asc(strSortArray(Y)) > Asc(strSortArray(Y + 1)) Then
' exchange the items
temp = strSortArray(Y)
strSortArray(Y) = strSortArray(Y + 1)
strSortArray(Y + 1) = temp
End If
}
{{
{ Next Y
Next X
lblOutput.Text = String.Join("", strSortArray)
}
End Sub
}
End Class
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.