Solved

Sorting a text file

Posted on 1998-08-19
5
164 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Cool, that worked perfectly. I can figure out the rest.

Thanks.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

17 Experts available now in Live!

Get 1:1 Help Now