anggalisdiyanto
New member
- Joined
- Jul 1, 2011
- Messages
- 3
- Programming Experience
- 1-3
I cannot sort a list of character on listbox. I just can sort a list of integer data type. I need help, here is my code :
Then, this is random string screenshot :
1. This is the random character generated with 5 letters and 10 words :
2. When i do sort, the result (blue rounded) is wrong. It's ugly :
I want to sort the words. Example :
Unsorted random words : zsdfg, akasf, plksn
Then i want to sort them :
Sorted words : akasf, plksn, zsdfg
Anyone can help me what is wrong with my code? I will very happy if there are any answers. Thanks in advance.
Dim langkah As Integer = 0 Private nilai2() As Integer Private Sub Quicksort2(ByVal list() As Integer, ByVal min As Integer, ByVal max As Integer) Dim rand As New Random Dim num_letters As Integer = _ Integer.Parse(txtNumLetters.Text) Dim num_words As Integer = _ Integer.Parse(txtNumWords.Text) Dim letters() As Char = _ "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray() Dim letter_num As Integer = rand.Next(0, _ letters.Length - 1) Dim word As String = "" ' Append the letter. word &= letters(letter_num) Dim angka_acak As New Random Dim nilai_tengah As Integer Dim hi As Integer Dim lo As Integer Dim i As Integer Dim a As Integer If min >= max Then Exit Sub i = rand.Next(min, max + 1) nilai_tengah = list(i) list(i) = list(min) lo = min hi = max Do Do While list(hi) >= nilai_tengah hi = hi - 1 a += 1 If hi <= lo Then Exit Do Loop If hi <= lo Then a += 1 list(lo) = nilai_tengah Exit Do End If list(lo) = list(hi) a += 1 lo = lo + 1 a += 1 Do While list(lo) < nilai_tengah lo = lo + 1 a += 1 If lo >= hi Then Exit Do Loop If lo >= hi Then lo = hi a += 1 list(hi) = nilai_tengah a += 1 Exit Do End If list(hi) = list(lo) a += 1 Loop Quicksort(list, min, lo - 1) a += 1 Quicksort(list, lo + 1, max) a += 1 Label1.Text = CStr(a) End Sub 'Sorting words in listbox1 Private Sub Button3_Click_1(sender As Object, e As EventArgs) Handles Button3.Click Dim rand As New Random Dim num_letters As Integer = _ Integer.Parse(txtNumLetters.Text) Dim num_words As Integer = _ Integer.Parse(txtNumWords.Text) Dim letters() As Char = _ "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray() Dim letter_num As Integer = rand.Next(0, _ letters.Length - 1) Dim word As String = "" ListBox3.Items.Clear() ' Append the letter. word &= letters(letter_num) Dim i As Integer Dim txt As String Dim jumlah As Integer = Val(txtNumLetters.Text) 'ListBox1.Items.Add("") 'ListBox1.Items.Add("Hasil pengurutan") 'ListBox1.Items.Add("") Quicksort2(nilai2, 0, num_letters) For letter_num = 1 To num_letters If nilai2(letter_num) < nilai2(letter_num - 1) Then Stop word &= letters(nilai2(letter_num)) ListBox3.Items.Add(word) Next letter_num End Sub Generate random words to listbox2 Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click Dim letter_num As Integer Dim num_letters As Integer = Integer.Parse(txtNumLetters.Text) Dim num_words As Integer = Integer.Parse(txtNumWords.Text) Dim letters() As Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray Dim rand As New Random() ListBox4.Items.Clear() ReDim nilai2(num_letters) For i As Integer = 1 To num_words Dim word As String = "" For j As Integer = 1 To num_letters ' Pick a random number between 0 and 25 ' to select a letter from the letters array. nilai2(letter_num) = rand.Next(0, letters.Length - 1) ' Append the letter. word &= letters(nilai2(letter_num)) Next j ' Add the word to the list. ListBox4.Items.Add(word) Next i End Sub
Then, this is random string screenshot :
1. This is the random character generated with 5 letters and 10 words :
2. When i do sort, the result (blue rounded) is wrong. It's ugly :
I want to sort the words. Example :
Unsorted random words : zsdfg, akasf, plksn
Then i want to sort them :
Sorted words : akasf, plksn, zsdfg
Anyone can help me what is wrong with my code? I will very happy if there are any answers. Thanks in advance.
Last edited: