Sort Program-How to?

I have more than 3 words I am trying to sort. (I am self learning programming and they say the sort is the best place to start)

Zebra
Transportation
And

My code is below. I am able to declare my variables, open a text file with the above words in it and write those words to another file. My problem lies in sorting them. The looping and if statement are giving me problems. Help! When I run the .vbs program I get an error that says line 19 char 5, input past end of file. I tryed doing something with (x + 1) but I am failing because of my lack of knowledge. Thankyou for helping!!

'=============================Step 1
Const ForReading = 1
Const ForWriting = 2
Dim SW
Dim Word1
Dim Word2
SW=0
Word1=0
Word2=0

'=============================Step 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForReading)

'=============================Step 3    
do until objFile.AtEndOfStream
    Word1 = objFile.Readline
       Wscript.Echo word1
    word2 = objFile.Readline
       Wscript.Echo word2
    If word1 > word2 then
    word2 = replace(word2, "word1", "word2")
    sw=1
    end if  
loop    

Set objFile = objFSO.OpenTextFile("D:\Temp\sort\test2.txt", ForWriting)    
    objFile.Write word2
    objfile.writeline
    objFile.Write word1
    objFile.Close  
tsi_adminAsked:
Who is Participating?
 
DarthModConnect With a Mentor Commented:
PAQed with points refunded (325)

DarthMod
Community Support Moderator
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is an implementation that uses QuickSort:

Option Explicit

Dim fileName
fileName = "C:\Documents and Settings\Michael\My Documents\1 VB Code\2 VBScript Testing\Test.txt"

Dim fso, f
Const ForReading = 1, ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(fileName) Then
   Set f = fso.OpenTextFile(fileName, ForReading, False)
   Dim lines ' convert file lines to an array after trimming trailing CrLfs
   lines = f.ReadAll
   f.Close
   While Right(lines, 2) = vbCrLf
       lines = Left(lines, Len(lines) - 2)
   Wend
   lines = Split(lines, vbCrLf)    

   ' sort the array using Quicksort
   QuickSort lines, LBound(lines), UBound(lines)

   ' Put array back together as one string
   lines = Join(lines, vbCrLf) & vbCrLf

   ' Overwrite the existing File with the sorted data
   Set f = fso.OpenTextFile(fileName, ForWriting, True)
   f.Write(lines)  
   f.Close

   MsgBox fileName, vbOKOnly, "Lines Have Been Sorted"
Else
   MsgBox fileName, vbOKOnly, "File Not Found"
End If



Private Sub Quicksort(ByRef list, ByVal min, ByVal max)
    Dim med_value
    Dim hi
    Dim lo
    Dim i

    ' If the list has no more than 1 element, it's sorted.
    If min >= max Then Exit Sub

    ' Pick a dividing item.
    i = Int((max - min + 1) * Rnd + min)
    med_value = list(i)

    ' Swap it to the front so we can find it easily.
    list(i) = list(min)

    ' Move the items smaller than this into the left
    ' half of the list. Move the others into the right.
    lo = min
    hi = max
    Do
        ' Look down from hi for a value < med_value.
        Do While list(hi) >= med_value
            hi = hi - 1
            If hi <= lo Then Exit Do
        Loop
        If hi <= lo Then
            list(lo) = med_value
            Exit Do
        End If

        ' Swap the lo and hi values.
        list(lo) = list(hi)
       
        ' Look up from lo for a value >= med_value.
        lo = lo + 1
        Do While list(lo) < med_value
            lo = lo + 1
            If lo >= hi Then Exit Do
        Loop
        If lo >= hi Then
            lo = hi
            list(hi) = med_value
            Exit Do
        End If

        ' Swap the lo and hi values.
        list(hi) = list(lo)
    Loop

    ' Sort the two sublists
    Quicksort list, min, lo - 1
    Quicksort list, lo + 1, max
End Sub
0
 
tsi_adminAuthor Commented:
This is what I came up with for a final answer.

Const ForReading = 1
Const ForWriting = 2
Dim SW
Dim Word1
Dim Word2
Dim Word3 'Tempholder variable
SW=1

Do until SW=0  
SW=0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objfile = objFSO.OpenTextFile("D:\Temp\sort\test.txt", ForReading)
Set objtest = objFSO.OpenTextFile("D:\Temp\sort\test2.txt", ForWriting)
Set objcopy = objfso.getfile ("D:\Temp\sort\test2.txt")

     Word1 = objFile.Readline
        'Wscript.Echo word1
  Do Until objFile.AtEndOfStream 'Check to see if EOF
     word2 = objFile.Readline
        'Wscript.Echo word2
     If word1 > word2 then 'Swaps alpebetical order of words
        word3=word1
        word1=word2
        word2=word3
        SW=1
     end if
     'If word1 = word2 then
       ' objtest.writeline
     'End if
    objtest.writeline (word1)
    Word1 = Word2
  loop
objtest.writeline (word2)
objfile.Close
objtest.Close
Objcopy.copy("D:\Temp\sort\test.txt")
Loop
Wscript.Echo "Completed sort"
Wscript.quit
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Did you not try my solution?...

It will work with ANY number of entries in your file.
0
 
tsi_adminAuthor Commented:
I did try it, thanks for the input, however, based upon my requirements for the project I had to go a different route.

Thanks to all for your input.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Good luck then... =)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.