Alphabetize Text File

You mean alphabetize its contents? Its lines? If lines then, read all lines of the file into a string array and then pass in the array into the shared Sort method of the Array object which will sort it for you.
 
I need it to alphabetize the text file when I click the button. By alphabetize I mean re-organize in alphabetical order by each line.
 
Code:
    Public Sub Alphabetize(ByVal file As String)
        Dim streamReader As New IO.StreamReader(file)
        Dim text As String = streamReader.ReadToEnd
        streamReader.Close()
        Dim line As String
        Dim lines() As String = Split(text, Environment.NewLine)
        lines.Sort(lines)

        Dim streamWriter As New IO.StreamWriter(file)

        For Each line In lines
            streamWriter.WriteLine(line)
        Next

        streamWriter.Close()
End Sub

Basically what mutant said.
 
Last edited by a moderator:
Public Sub Alphabetize(ByVal file As String)

Dim streamReader As New IO.StreamReader(TextBox2.Text)
Dim text As String = StreamReader.ReadToEnd
StreamReader.Close()
Dim line As String
Dim lines() As String = Split(text, Environment.NewLine)
lines.Sort(lines)

Dim StreamWriter As New IO.StreamWriter(TextBox2.Text)

For Each line In lines
StreamWriter.WriteLine(line)
Next

StreamWriter.Close()
End Sub

===================

This does not work...Was that the whole code or am I missing something?
 
mutant said:
What does not work? What line? Does it throw an error or simply doesnt do what you want it to do?


There are no errors, it just doesnt do anything. I want it to alphabetize the text file and it is not doing anything.
 
PureSc0pe said:
There are no errors, it just doesnt do anything. I want it to alphabetize the text file and it is not doing anything.
If you look back on your length of file thread i included an example an how to sort an array. The real easy way too. It will alphabetize.
 
Last edited by a moderator:
Back
Top