I can't quite seem to get my head around this, i can load numbers in and add to an array then sort via bubble-sort but i cant seem to manage it with words.

Any examples or help would be nice!! I need to load the word list from file then add them to an array, then sort them with bubble sort.

Thanks!

Recommended Answers

All 7 Replies

What have you done so far? Reading words from a file into an array is simple but how you do it depends on the format of the file. Considerations are:

  1. is there one word per line or multiple words
  2. are there non-word characters (punctuation) to be accounted for
  3. if multiple words, are they blank separted or comma separated
  4. if blanks, could there be more than one blank between words

Can you give us some sample input?

Thanks for replying, right now in the read file button i have

Dim strWordArray(5) As String
Dim i As Integer = 0
        If System.IO.File.Exists(myFileName) = True Then
            Dim objReader As New System.IO.StreamReader(myFileName)
            Do While objReader.Peek() <> -1
                strWordArray(i) = objReader.ReadLine()
                i = i + 1
                TextBox1.Text = strWordArray
            Loop
        Else
            MsgBox("File does not exist!")
        End If

this code is somewhat messed up now after i have been trying different things :/

The word list is formatted as follows

feculency
daneflower
chiefdom
demast
ninon

Just random words like that.

The sorting should be pretty much the same as you use for the numbers.

The following is used for the numbers

Dim BSortFileName As String = "BSort.txt"
        Dim x, y, temp As Integer
        Dim i As Integer

        For y = 0 To UBound(strWordArray) - 2
            For x = 0 To UBound(strWordArray) - 2
                If strWordArray(x) > strWordArray(x + 1) Then
                    temp = strWordArray(x + 1)
                    strWordArray(x + 1) = strWordArray(x)
                    strWordArray(x) = temp
                End If
            Next
        Next

        If System.IO.File.Exists(myFileName) = True Then
            Dim objWriter As New System.IO.StreamWriter(BSortFileName)
            For i = 0 To 199999
                objWriter.Write(strWordArray(i) & vbCrLf)
            Next
            objWriter.Close()
            MsgBox("Sorted")
        Else
            MsgBox("File not exist")
        End If

Whats needed to be changed in order to be able to sort strings?

As long as it works for numbers it should work as is for strings.

How thou? i have gotten errors such as "Can't convert integer to string" or what ever it would be.

The easiest way to read one word per line is

Dim words() As String = System.IO.File.ReadAllLines(filename)

As long as it works for numbers it should work as is for strings.

Only if you declare temp as string instead of integer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.