Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a Fixed Length Text File

Status
Not open for further replies.

VisualGuy

Programmer
May 27, 2003
162
US
Does someone have an example of the best way to sort a Fixed Length Flat File using VB.NET? In the past, I've done this by writing it into a database table and then reading it back sorted, but that is not an option for what I want to do here.

I need to read it from one text file and then write it out, sorted by one of the fields, to another. Any help would be much appreciated...
 
I found a shell command, but what about internal VB.NET? There must be a simple way to do this:

sort /+40 MyTextFile.TXT > MyNewTextFile.TXT

This sorts column 40 in asc order into MyNewTextFile.TXT.
 
There are several sorting routines availible, the one you will need depends on the number of records. If the file is small i would suggest bubble sort. For a larger file, something like a has sort would be better.

a sample of bubblesort from wikisource, you will have to modify the code to your own purpose and I suggest you only use it if you have 1,000 records or less.

Code:
Sub Bubblesort(Array() as Integer, Length as Integer)
   Dim I as Integer
   Dim J as Integer
   Dim Temp as Integer

   For I = Length -1 To 1 Step -1
      For J = 0 to I - 1
         IF Array(J)>Array(J+1) THEN  ' Compare neighboring elements
            Temp = Array(j) 
            Array(J) = Array(J+1)
            Array(J+1) = Temp
         End If
      Next J
   Next I

End Sub

if it is to be it's up to me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top