In: Computer Science
How do I make this sort in true alphabetical order instead of ascii(ABCabc) order? I am trying to get AaBbCc. Currently I get ABCabc. please help... here is the code using VB:
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
I Have modified the code as below. Basically you need to add the inbuilt function Array.Sort(strSortArray)
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)
Array.Sort(strSortArray)
Next X
lblOutput.Text = String.Join("", strSortArray)
End Sub
End Class
OUTPUT
