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)


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

'=============================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")
    end if  

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

Community Support Moderator
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
   While Right(lines, 2) = vbCrLf
       lines = Left(lines, Len(lines) - 2)
   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)

   MsgBox fileName, vbOKOnly, "Lines Have Been Sorted"
   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
        ' Look down from hi for a value < med_value.
        Do While list(hi) >= med_value
            hi = hi - 1
            If hi <= lo Then Exit Do
        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
        If lo >= hi Then
            lo = hi
            list(hi) = med_value
            Exit Do
        End If

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

    ' Sort the two sublists
    Quicksort list, min, lo - 1
    Quicksort list, lo + 1, max
End Sub
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

Do until 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
     end if
     'If word1 = word2 then
       ' objtest.writeline
     'End if
    objtest.writeline (word1)
    Word1 = Word2
objtest.writeline (word2)
Wscript.Echo "Completed sort"
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.
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.
Mike TomlinsonMiddle School Assistant TeacherCommented:
Good luck then... =)
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.