Solved

Sorting a text file

Posted on 1998-08-19
5
165 Views
Last Modified: 2010-04-30
I want to be able to sort a text file alphabetically using vb code.  I've never had to do sorting before, anyone know an easy way of doing it?  code will help.
0
Comment
Question by:dokken
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:richtsteig
ID: 1429917
I think the best way to do so depends on the size of your text file. Upto how many lines are we talking about? Do you want to save your sorted text back to the original file?

A very simple way (cause you don't need to find or implement a sort algorithm) is to import the file as an one-field-access table with an index on that field and export it afterwards. This can be done with a minimum of code.

If you don't want to use a database
You can read your file into an array of strings sort the array and write the result back to a file.

A very "dirty" (the most dirtiest I think) algorithm is to itereate through the array, find the smallest value and swap the actual with the smallest. (I don't know exactly, but I think this is the "bubble sort", next better would be the "quick sort")

Sub SortArray(v As Variant) ' <- v is your array

Dim i As Long
Dim j As Long
Dim tmp As Variant
Dim actmin As String
Dim minidx As Long

For i = LBound(v) To UBound(v) - 1
   actmin = v(i)
   minidx = i
   For j = i + 1 To UBound(v)
      If v(j) < actmin Then
         actmin = v(j)
         minidx = j
      End If
   Next j
   
   If minidx <> i Then
      tmp = v(i)
      v(i) = v(minidx)
      v(minidx) = tmp
   End If
   
Next i

End Sub

You can use a type of sort algorithm which does all the sorting on your disk instead of "wasting" memory. It splits your file into several temporary files depending on the read values and then merges the files together. This works fine for a very very large amonut of data.


0
 
LVL 12

Expert Comment

by:mark2150
ID: 1429918
Simplest way is at DOS prompt:

SORT < UNSORTED > SORTED

This will take the UNSORTED file and rank it line by line into the SORTED file, naturally you can use any filename you want.

This is good to 30,000 lines or so

You can call it inside of VB with:

SHELL("SORT < " & SourceFile & " > " & ResultFile, 1)

Where SourceFile and ResultFile are text strings containing valid file paths.
0
 

Author Comment

by:dokken
ID: 1429919
richtsteig,

I haven't had a lot of experience using array's so I'm not sure how to input a text file using and array and then sorting it, then outputing it back to the original file.  If you can show some code that would help alot!


Mark2150,

Can't use the shell to sort... I'm writing a WinCGI script.  I tried that exact line before and it doesn't work.

0
 
LVL 2

Accepted Solution

by:
richtsteig earned 40 total points
ID: 1429920
OK dokken, her it is:


Sub InputTextFile(filename as string)

Dim NumOfLines As Integer
Dim txtlines() As String 'your array of strings
Dim curLine As Long
dim i as long

Open filename For Input As #1
curline = 0
NumOfLines = 100 'initial number of rows
ReDim txtlines(NumOfLines)

Do While Not EOF(1)
   
   'enlarge array by 100 (if running out)
   If curline > UBound(txtlines) Then
      ReDim Preserve txtlines(UBound(txtlines) + NumOfLines)
   End If
   
   Line Input #1, txtlines(curline)
   curline = curline + 1
Loop

Close #1

ReDim Preserve txtlines(curline - 1) 'cutting the rest
Call ArraySort(txtlines)  ' I posted last time

'test the result
For i = 0 To curline - 1
   MsgBox txtlines(i)
Next

Exit Sub


0
 

Author Comment

by:dokken
ID: 1429921
Cool, that worked perfectly. I can figure out the rest.

Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now