Hello guys,

I did appreciate this quick assistance, I have a .csv file and I want the lines in the file to be sorted using value in the first column and the write out the sorted file to another file

Take an instance we have:

36,Line1 APPLE FRUIT
11,Line1 GOAT DOMESTIC
36,Line1 TABLE HOUSE
11,Line1 EAGLE BIRD
05,Line1 BIBLE CHURCH
03,Line1 LION WILD
11,Line1 COMPUTER OFFICE
11,Line1 SHIRT WARDROBE
03,Line1 BOOK SCHOOL

and I want my output file to be

03,Line1 LION WILD
03,Line1 BOOK SCHOOL
05,Line1 BIBLE CHURCH
11,Line1 COMPUTER OFFICE
11,Line1 SHIRT WARDROBE
11,Line1 EAGLE BIRD
11,Line1 GOAT DOMESTIC
36,Line1 TABLE HOUSE
36,Line1 APPLE FRUIT

Thank you in advance

Recommended Answers

All 12 Replies

You can use a sorted set as

Dim sort As New SortedSet(Of String)
Dim sw As New System.IO.StreamWriter("d:\temp\test2.csv")

For Each line As String In System.IO.File.ReadAllLines("d:\temp\test1.csv")
    sort.Add(line)
Next

For Each line As String In sort
    sw.WriteLine(line)
Next

sw.Close()

Dear Reverend,

I found error from the 1st line "Dim sort As New SortedSet(Of String)" and dont know if I need to import anything for that to work well.
Moreso, where will I specify that the sorting value should the first field in the file.

Thank you in advance.

You don't have to import anything or add any special references. It works fine for me. What error are you getting? I assumed based on the sample data you gave that the fields are fixed width. As such you don't have to specify the first field as the sort field. You can just sort based on the entire line.

Thank you Reverend,

Below is my code perhaps you can help from there:

    'Destination is my path
    'Comfyle is a .csv file am reading from

    Dim AllNewRec As String = ""
    Dim NwSn As Integer = 0
    Dim FinWriter As StreamWriter = New StreamWriter(Destination & "\" & "LineTest_Final.dat")
    Using AllNwlinR As New StreamReader(Destination & "\" & ComFyle)
        Do Until AllNwlinR.EndOfStream
            Dim gdline As String = AllNwlinR.ReadLine
            Dim SortKod As String = gdline.Substring(20, 2)
            Dim MinusSN As String = gdline.Substring(6)     

            'Here I have the BrachCode and eachline without the serialno as array into mnewline
            Dim mnewline() As String = {SortKod + "," + MinusSN}
            For Each valu As String In mnewline
                AllNewRec &= valu + vbNewLine
            Next
        Loop
    FinWriter.Write(AllNewRec)
    FinWriter.Dispose()
    FinWriter.Close()

Now looking at your response I think its better if I sort the "mnewline" before adding everything into the final output file. Though I was initially writing out "AllNewRec" and then read it with another streamreader like you have in your code snippet, doing that now I cannot move forward because of the error:
Type 'SortedSet(Of String)' is not defined.
Change 'SortedSet(Of String)' to 'SortedList(Of String)'
Change 'SortedSet(Of String)' to 'SortedOrder(Of String)'"

Thank you in anticipation.

While I'm looking can you please tell me what version of VB you are running? That might explain why you get an error and I don't. I have Visual Studio 2010.

I am running on Visual Studio 2012 Professional

Do you have the latest .NET framework installed? It may be that the SortedSet is a recent addition that you don't have yet. You can also do the sort by

Dim lines() As String = System.IO.File.ReadAllLines("d:\temp\test1.csv")
Array.Sort(lines)

This should work on your version of the framework

Reverend,

You have ease my day.. GLORY BE TO JESUS.
Before I post the final code I inserted for other to benefit from, the first line of the output file did not arrange with others. I mean it omitted the firstline while doing the sorting. Any help on why dis is happening?

Post the contents of the input file as well as the output file because running your code against the input you posted at the top of this thread just results in an error. If the output isn't what you want then post what it should look like. Also, I have no idea what you are trying to do based on the code you posted. Also, the comment Here I have the BrachCode and eachline without the serialno as array into mnewline doesn't mean anything to me.

Okay Reverend,
Been simple enough let me post my code here

    Dim MxFile as string = ""
    Dim sw1 As New StreamWriter(Destination & "\" & "SMat.inp")
    Dim lines() As String = System.IO.File.ReadAllLines(Destination & "\" & "LineTest_Final.inp")
    For Each mlin As String In lines
        Array.Sort(lines)
        MxFile &= mlin + vbNewLine
    Next
    sw1.Write(MxFile)
    sw1.Dispose()
    sw1.Close()

My input file is just same thing as I have in my question from beginning
And my output file arrange the whole line but the first line is not in still remains where it is whereas it should find its place somewhere else. This is what I mean I now get:
36,Line1 APPLE FRUIT
03,Line1 LION WILD
03,Line1 BOOK SCHOOL
05,Line1 BIBLE CHURCH
11,Line1 COMPUTER OFFICE
11,Line1 SHIRT WARDROBE
11,Line1 EAGLE BIRD
11,Line1 GOAT DOMESTIC
36,Line1 TABLE HOUSE

Instead of
03,Line1 LION WILD
03,Line1 BOOK SCHOOL
05,Line1 BIBLE CHURCH
11,Line1 COMPUTER OFFICE
11,Line1 SHIRT WARDROBE
11,Line1 EAGLE BIRD
11,Line1 GOAT DOMESTIC
36,Line1 TABLE HOUSE
36,Line1 APPLE FRUIT

Sincerely am happy with this assistance and I will like to mark this thread solved though I still need to fix the first line not been in its correct place.

That code doesn't have anything to do with the code you posted previously so let's just worry about this code.

Dim lines() As String = System.IO.File.ReadAllLines(Destination & "\" & "LineTest_Final.inp")

reads the entire file into an array so you don't need a loop. Likewise

Array.Sort(lines)

sorts the entire array at once so you, again, do not need a loop. Furthermore, you can write the entire array of lines to the file in one line. So the code becomes (I'll just use my local test file names)

Imports System.IO
    .
    .
    .
    Dim lines() As String = File.ReadAllLines("d:\temp\test1.csv")
    Array.Sort(lines)
    File.WriteAllLines("d:\temp\test2.csv", lines)

Reverend !

GOD Bless you and all your effort.
It worked perfectly.

Thank you.

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.