Search code examples
vb.netsortingbubble-sortbasic

Why is my program not sorting properly?


I have a problem with my program, the program is supposed to ask the user for the drivers times, the user will input the drivers time, and the program is supposed to sort it from fastest to slowest and then display them in the order. But when i try and run the program, the drvers time is sorted but the drivers name have not been sorted.

Example:

what the user must input into the program.

Drivers name         drivers time

Sebastian Williams        10
Tom Hamilton              6
Danny Ricardo             2
Walter Borras             7
Fernando Sonal            1
Jenson Smith              9

what the program outputs

  1        Sebastian Williams        
  2        Tom Hamilton              
  6        Danny Ricardo             
  7        Walter Borras             
  9        Fernando Sonal            
  10       Jenson Smith    

as you can see, the times have been sorted but the names have not been sorted with the times. how would i be able to fix this?

here is my code.

Public Class Form1
Public Sub cmdStart_Click(sender As Object, e As EventArgs) Handles cmdStart.Click

    Dim name
    Dim racetime
    Dim team

    input(Name, raceTime, team)

End Sub
Public Structure raceCar

    Public Name() As String
    Public raceTime() As Double
    Public team() As String

End Structure
Private Sub input(ByRef name(), ByRef raceTime(), ByRef team())

    Dim raceCars As raceCar
    ReDim raceCars.Name(5)
    ReDim raceCars.raceTime(5)
    ReDim raceCars.team(5)

    Dim filenameInput As String = "G:\grandPrixVB\Input\Input.csv"
    Dim textfileInput As New System.IO.StreamReader(filenameInput)
    Dim filenameOutput As String = "G:\grandPrixVB\Input\Input2.txt"
    Dim textFileOutput As New System.IO.StreamWriter(filenameOutput)

    For counter = 0 To 5

        raceCars.Name(counter) = textfileInput.ReadLine
        raceCars.team(counter) = textfileInput.ReadLine
        textfileInput.ReadLine()
        textfileInput.ReadLine()

        raceCars.raceTime(counter) = InputBox("Enter the race time for " & raceCars.Name(counter) & ".")

        ListBoxUnsorted.Items.Add(raceCars.Name(counter) & " is part of " & raceCars.team(counter) & " got a time of " & raceCars.raceTime(counter) & " seconds.")
        textFileOutput.WriteLine(raceCars.Name(counter) & "," & raceCars.raceTime(counter))
    Next

    textfileInput.Close()
    textfileInput.Dispose()
    textFileOutput.Close()
    textFileOutput.Dispose()


    Dim i As Integer


    ListBoxSorted.Items.Add("Unordered Array")

    For i = LBound(raceCars.raceTime) To UBound(raceCars.raceTime)
        ListBoxSorted.Items.Add(raceCars.raceTime(i))
    Next

    sortArray(raceCars.raceTime)

    ListBoxSorted.Items.Add("")
    ListBoxSorted.Items.Add("Ordered Array")

    For i = LBound(raceCars.raceTime) To UBound(raceCars.raceTime)
        ListBoxSorted.Items.Add(raceCars.raceTime(i) & raceCars.Name(i))
    Next

End Sub
Private Sub sortArray(ByRef array() As Double)

    Dim i As Double
    Dim j As Double
    Dim minimum As Double
    Dim swapValue As Double
    Dim upperBound As Double
    Dim lowerBound As Double
    lowerBound = LBound(array)
    upperBound = UBound(array)

    For i = lowerBound To upperBound
        minimum = i
        For j = i + 1 To upperBound

            If array(j) < array(minimum) Then
                minimum = j
            End If
        Next j

        If minimum <> i Then
            swapValue = array(minimum)
            array(minimum) = array(i)
            array(i) = swapValue

        End If
    Next i

End Sub

End Class

thanks in advance


Solution

  • You are only sorting the raceTimes array. You will need to do the same for your other arrays in your raceCar variable, but luckily you can piggy back off of your existing logic:

    First you'll need to pass your other arrays in to sortArray method and create "Swap Variables" for each:

    Private Sub sortArray(ByRef array() As Double, ByRef names() As String, ByRef teams() as String)
    
    Dim swapName As String
    Dim swapTeam As String
    

    Then when you swap your time values you can also swap your names and teams:

    If minimum <> i Then
        swapValue = array(minimum)
        swapName = names(minimum)
        swapTeam = teams(minimum)
        array(minimum) = array(i)
        names(minimum) = names(i)
        teams(minimum) = teams(i)
        array(i) = swapValue
        names(i) = swapName
        teams(i) = swapTeam
    End If