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
:icon_cry:

Dani AI

Generated

A reliable approach is to parse the file into message objects (one per CMGL header), store the header fields and the lines that follow as that message's payload, then sort the objects by the numeric CMGL index. is trying to sort by the CMGL serial and display the quoted header fields separately from the payload; correctly prompted for details. Read the file into lines (use File.ReadAllLines) and detect header lines with a regular expression (see Regex). For each header start a new message, extract the quoted fields, then append subsequent lines as that message's payload until the next header.

The following VB.NET pattern (VB2005/.NET 2.0 compatible) shows the parsing, grouping and sorting strategy. Implement a small message class that implements IComparable(Of T) so List.Sort orders by the numeric index (see IComparable(Of T)):

Imports System
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Collections.Generic

Public Class SmsMessage
    Implements IComparable(Of SmsMessage)

    Public Index As Integer
    Public HeaderFields As New List(Of String)()
    Public Payload As String = String.Empty

    Public Function CompareTo(other As SmsMessage) As Integer Implements IComparable(Of SmsMessage).CompareTo
        Return Me.Index.CompareTo(other.Index)
    End Function
End Class

' ... inside Main: read lines, match header regex, collect quoted fields, append payload lines, then messages.Sort()

Notes: use Integer.TryParse to avoid exceptions on malformed headers; handle empty quoted fields explicitly; if payload is hex, convert pairs with Convert.ToByte(hexPair,16) and then Encoding.ASCII.GetString(bytes) to decode; for very large files prefer StreamReader and process incrementally instead of ReadAllLines. If header variants exist, post a sample header line so the regex can be adjusted.

Recommended Answers

All 3 Replies

Member Avatar for Member #46692

Basic string parsing and sorting, any particular problem you were having.

its sorting problem

Member Avatar for Member #46692

I'm not quite sure what you mean by sorting you'll have to elaborate.

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.