Click here to Skip to main content
15,886,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am saving stock info such as product ID,Product name,price and quantity. I want to be able to sort by product ID using a bubble sort.
I am first loading the data into the list box from the relevant text file and then sorting.
I have tried using the one below but it doesn't not seem to work.Thanks in advance.
"StringBubbleSort not declared"

What I have tried:

Dim STUDENT_FILE As String = ("C:\Users\Windows 7 User\Desktop\Stock.txt")
        Dim objReader As New System.IO.StreamReader(STUDENT_FILE)

        Dim strDataline As String 'Data line
        Dim strArr(2) As String ' Array for bits of line
        Dim blfound As Boolean
        ListBox1.Items.Clear()

        Do While objReader.Peek() <> -1 'read the file till the end.
            strDataline = (objReader.ReadLine()) ' read line and store into variable
            strArr = strDataline.Split(",") 'Split line and put into array
            ListBox1.Items.Add(strDataline)
            blfound = True

        Loop
        MsgBox("Stock data has been loaded")
        objReader.Close()
        If blfound = False Then MsgBox("stock data has not been found")

         'Bubble sort'
        Dim arr(ListBox1.Items.Count - 1) As String
        Dim i As Integer

        For i As Integer = 0 To arr.Length - 1


            arr(i) = CStr(ListBox1.Items(i))
        Next
        For i As Integer = 0 To arr.Length - 1
            For j As Integer = 0 To arr.Length - 2 - i
                If String.Compare(arr(j), arr(j + 1)) > 0 Then
                    Dim temp As String = arr(j)
                    arr(j) = arr(i)
                    arr(i) = temp
                End If
            Next
        Next
        StringBubbleSort(arr)
        ListBox1.Items.AddRange(arr)
Posted
Updated 2-Mar-18 21:47pm

1 solution

VB
StringBubbleSort(arr)

You have not defined that subroutine anywhere.
 
Share this answer
 
Comments
faiqaa 3-Mar-18 3:50am    
Thanks for your time, would this work without the subroutine?
Richard MacCutchan 3-Mar-18 3:51am    
Try it.
faiqaa 3-Mar-18 8:14am    
I tried using this code, below the displaying one shown above, but instead of sorting it just displays the data already shown in the listbox twice.
Richard MacCutchan 3-Mar-18 8:22am    
Why not let the ListBox sort itself? See Using the List Box Control[^].
faiqaa 3-Mar-18 8:54am    
The code below has worked on a list of only numbers, now the problem is that I am storing stock details therefore I wanted to sort according to the product ID, as soon as I write something like this, 4564,Samsung galaxy note 5,£600,15 ( the actual data I am saving) this sort does not sort at all..

Dim sorted As Boolean = False

Dim t As String

Do

sorted = True

For i As Integer = 0 To ListBox1.Items.Count - 2

If CDbl(ListBox1.Items(i).ToString) > CDbl(ListBox1.Items(i + 1).ToString) Then

t = ListBox1.Items(i).ToString

ListBox1.Items(i) = ListBox1.Items(i + 1)

ListBox1.Items(i + 1) = t

sorted = False

End If

Next

If sorted Then

Exit Do

End If

Loop

Error: Additional information: Conversion from string "4564,Samsung galaxy note 5,£600,15" to type 'Double' is not valid.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900