title: Bubble Sort logical error VB.NET
body: <p>This program suppose to sort records(in arySort) in ascending order by last name(index 1 in aryTemp and aryTemp2) and place the result in the list box over the old, preloaded, unsorted records.</p>
  
  <p>It sorts them strangely, I have to click multiple times the Ascending button to get the actual sort result that I suppose to get from clicking the button once.</p>
  
  <p><em>Why doesn't it sort items with a single mouse click?</em></p>
  
  <p>The source:</p>
  
  <pre><code>Public Class Form1
      Dim FILE_NAME As String = "Students.txt"
      Dim numberOfRecords As Integer 'total number of records
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          If System.IO.File.Exists(FILE_NAME) = True Then
              Dim objReader As New System.IO.StreamReader(FILE_NAME)
              Do While objReader.Peek() &lt;&gt; -1
                  lstBox.Items.Add(objReader.ReadLine)
                  numberOfRecords += 1
              Loop
              objReader.Close()
          End If
      End Sub
  
      Private Sub btnAscending_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAscending.Click
          'load all students into array
          Dim arySort(numberOfRecords - 1) As String
          Dim aryTemp() As String 'holds first record's last name
          Dim aryTemp2() As String 'holds second record's last name
          For i = 0 To numberOfRecords - 1
              arySort(i) = lstBox.Items(i)
          Next
          Dim temp As String 'holds temporary record
          Dim k As Integer
          For i = 0 To arySort.Length - 2
              aryTemp = Split(arySort(i), " ")
              For k = i + 1 To arySort.Length - 1
                  aryTemp2 = Split(arySort(k), " ")
                  If aryTemp(1) &lt; aryTemp2(1) Then
                      temp = arySort(k)
                      arySort(k) = arySort(i)
                      arySort(i) = temp
                  End If
              Next
          Next
          lstBox.Items.Clear()
          numberOfRecords = 0
          For i = 0 To arySort.Length - 1
              lstBox.Items.Add(arySort(i))
              numberOfRecords += 1
          Next
      End Sub
  End Class
  </code></pre>