Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting a text file

Posted on 1998-08-19
5
Medium Priority
?
184 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 160 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…
Suggested Courses

876 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