Solved

Sorting a text file

Posted on 1998-08-19
5
171 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

739 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