Is there a problem with array.sort
If I load an array in to a list box I get
A is Element [0] in the array
B is Element [1] in the array
C is Element [2] in the array
but when I click the button to sort the array I get
is Element [0] in the array
A is Element [1] in the array
B is Element [2] in the array
Here is the code,
Private Sub btnShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAll.Click
ListBox1.Items.Clear()
For i = 0 To 2
ListBox1.Items.Add(arrString(i) & " is element [" & i & "] in the array")
Next
End Sub
Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
ListBox1.Items.Clear()
Array.Sort(arrString)
For i = 0 To 2
ListBox1.Items.Add(arrString(i) & " is element [" & i & "] in the array")
Next
End Sub

What the Array is going on?
John Perks
My bad...........I was using an ArrayList instead of a regular Array. Funny thing, if you declare an Array like this: Dim arrString() As Array = (a,b,c)
VB gripes about it but, it will run and work correctly.
Hmmmmmmmmm could be a bug or I'm not understanding something!!!
james
aka:Trucker
Taylor Brown
GDM
sundanceca
You can view a screenshot here,tried to use the Insert Link thing but does not appear to work. http://www.rosette.org.uk/Pics/Screenshot.gif
Public Class AnimalArray
Dim arrAnimal(6) As String
Dim AnimalIndex As Integer
Dim i As Integer
Private Sub AnimalArray_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
arrAnimal(0) = RadioButton1.Text
arrAnimal(1) = RadioButton2.Text
arrAnimal(2) = RadioButton3.Text
arrAnimal(3) = RadioButton4.Text
arrAnimal(4) = RadioButton5.Text
arrAnimal(5) = RadioButton6.Text
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
LblDisplay.Text = "The " & RadioButton1.Text & Chr(13) & "is Element [" & (Array.IndexOf(arrAnimal, RadioButton1.Text)) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
LblDisplay.Text = "The " & RadioButton2.Text & Chr(13) & "is Element [" & (Array.IndexOf(arrAnimal, RadioButton2.Text)) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
LblDisplay.Text = "The " & RadioButton3.Text & Chr(13) & "is Element [" & (Array.IndexOf(arrAnimal, RadioButton3.Text)) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
LblDisplay.Text = "The " & RadioButton4.Text & Chr(13) & "is Element [" & (Array.IndexOf(arrAnimal, RadioButton4.Text)) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
LblDisplay.Text = "The " & RadioButton5.Text & Chr(13) & " is Element [" & (Array.IndexOf(arrAnimal, RadioButton5.Text)) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub RadioButton6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton6.CheckedChanged
LblDisplay.Text = "The " & RadioButton6.Text & Chr(13) & " is Element [" & (Array.IndexOf(arrAnimal, RadioButton6.Text)) & "]" & Chr(13) & "in the Array"
End Sub
Private Sub btnShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAll.Click
ListBox1.Items.Clear()
For i = 0 To 5
ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")
Next
End Sub
Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
ListBox1.Items.Clear()
Array.Sort(arrAnimal)
For i = 0 To 5
ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")
Next
End Sub
Private Sub btnReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplace.Click
arrAnimal.SetValue(txtReplace.Text, cmbArrNum.SelectedIndex)
Select Case cmbArrNum.SelectedIndex
Case 0
RadioButton1.Text = txtReplace.Text
Case 1
RadioButton2.Text = txtReplace.Text
Case 2
RadioButton3.Text = txtReplace.Text
Case 3
RadioButton4.Text = txtReplace.Text
Case 4
RadioButton5.Text = txtReplace.Text
Case 5
RadioButton6.Text = txtReplace.Text
End Select
ListBox1.Items.Clear()
For i = 0 To 5
ListBox1.Items.Add(arrAnimal(i) & " is element [" & i & "] in the array")
Next
End Sub
End Class
Paul Marriott
If I set the Breakpoint to the start of the Click Event and type:
arrString(3) I get a runtime error. So, what are you telling me
Unless otherwise declared, Arrays (well, I used an Arraylist to test this, maybe, that's the difference) are Zero based. If I type arrString(0) or arrString(1) or arrString(2) I get all the elements in the Arraylist.....................
james
aka:Trucker
Alexxey
Dim arrAnimal(5) As String
to give it 6 elements. VB quirk, tough when you came from "C".
willrobinson
This works:
Public arrstring(2) As String Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Loadarrstring(0) =
"A"arrstring(1) =
"B"arrstring(2) =
"C" End Sub Private Sub BtnShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnShowAll.ClickListBox1.Items.Clear()
For i As Integer = 0 To 2ListBox1.Items.Add(arrstring(i) &
" is element [" & i & "] in the array") Next End Sub Private Sub Sort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sort.ClickListBox1.Items.Clear()
Array.Sort(arrstring)
For i As Integer = 0 To 2ListBox1.Items.Add(arrstring(i) &
" is element [" & i & "] in the array") Next End Subsimonoch
I could be crazy of course but.....
It looks to me as if you are putting them in a sorted order and then sorting them and seeing nor difference and wondering why.
wzaqma
ListBox1.Items.Clear()
Me.arrString.Sort() For i = 0 To 2ListBox1.Items.Add(arrString(i) &
" is element [" & i & "] in the array") Next End SubThe problem is in the line you had as Array.Sort(arrString) I fixed it by doing:
me.arrString.Sort()
james
aka:Trucker
Edit:
Or just put: arrString.Sort() I sometimes like to use Me.
pietpeters
When loaded the array is unsorted
SathikKhan
zoltak
So does this:
Dim
arrString() As String = {"a", "c", "b"} Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Private Sub btnShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAll.Click Dim i As IntegerListBox1.Items.Clear()
For i = 0 To 2ListBox1.Items.Add(arrString(i) &
" is element [" & i & "] in the array") Next End Sub Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click Dim i As IntegerListBox1.Items.Clear()
Array.Sort(arrString)
' arrString.Sort() For i = 0 To 2ListBox1.Items.Add(arrString(i) &
" is element [" & i & "] in the array") Next End SubAre we having fun yet or what!!!!
james
aka:Trucker
Benjamin Chebrou
Yeah James,
I still have a thing about not using those dang braces.
People might think I 'm into the 'C' language, and my whole purpose in life would become {{{{{{{{{{{{{{{{{{{VOID}}}}}}}}}}}}}}}}}}}}.
shanku