Read Text File through console application and sort data

bcm

Well-known member
Joined
Aug 13, 2007
Messages
56
Programming Experience
Beginner
I have written a console application in VB.NET2005 which will read a text file and its contents.

But now I want to sort its data on the basis of its contents i.e the text file has following contents:::

CMGL: 1,"REC READ","7=4;;1413818891>53:1",,"07/12/30,12:18:43+22"
0B05040B840B840003CB0201570601AE02056A0045C60C033230322E3133382E3130302E36302F73
6875726C3F643D3536000AC3072007122913381010C3072008010813381001033130206C616B6820
434153482C203435204E6F6B696120363330302070686F6E65732026206C6F616473206F66207461
6C6B74696D652120546F2077696E2C20616E7377

Here I want to sort it by CMGL:1 which is the serial no. of data and so on such as CMGL:2.. etc
I also want to show th data in each " "(inverted commas) seperately whenever it gets CMGL: and the data below it seperately
Please Help soon
:confused:
 
Start with inserting the snippet "Read a Delimited Text File", then you can analyze the fields; if first field starts with "CMGL:".
Write a class/structure that have a field/property for each data field, keep a list/collection/array of this to store instances.
Implement IComparable for sorting.

Is that from second line in your quote one long string or several lines? Anyway here's the suggestions implemented that will work for both:
VB.NET:
Class thedata
    Implements IComparable(Of thedata)

    Public id As Integer
    Public fields() As String
    Public datalines As New List(Of String)

    Public Function CompareTo(ByVal other As thedata) As Integer _
    Implements System.IComparable(Of thedata).CompareTo
        Return Me.id < other.id
    End Function
End Class
VB.NET:
Sub DoWork()
    Dim datas As New List(Of thedata)
    Dim data As thedata = Nothing
    Dim filename As String = "Test.txt"
    Dim fields As String()
    Dim delimiter As String = ","
    Using parser As New TextFieldParser(filename)
        parser.SetDelimiters(delimiter)
        While Not parser.EndOfData
            ' Read in the fields for the current line
            fields = parser.ReadFields()
            ' Add code here to use data in fields variable.
            If fields(0).StartsWith("CMGL:") Then
                data = New thedata
                data.id = CInt(fields(0).Split(" "c)(1))
                Array.Resize(data.fields, fields.Length - 1)
                Array.Copy(fields, 1, data.fields, 0, data.fields.Length)
                datas.Add(data)
            Else
                datas(datas.Count - 1).datalines.Add(fields(0))
            End If
        End While
    End Using
    datas.Sort()
    For Each data In datas
        Console.WriteLine("CMGL: " & data.id)
        For Each field As String In data.fields
            Console.WriteLine(field)
        Next
        For Each dataline As String In data.datalines
            Console.WriteLine(dataline)
        Next
    Next
End Sub
TextFieldParser is of Microsoft.VisualBasic.FileIO namespace, this namespace is imported automatically when you insert the mentioned snippet.
 
Back
Top