Solved

Sorting a text file

Posted on 1998-08-19
5
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…
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…
Suggested Courses
Course of the Month3 days, 17 hours left to enroll

630 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