Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy to understand sorting

File and Data Processing

Easy to understand sorting

by  markdmac  Posted    (Edited  )
While trying to make heads or tails out of examples of sorting arrays I decided that despite some great examples, the whole concept of using ADO was beyond my own current understanding and as such probably beyond the comprehension of most new scripters visiting this forum.

I decided to seek a more "pure" vbscript solution. Along the way I discovered that the whole thing can be done with just one line of code in jscript, but again I was looking for a pure vbscript solution. I was lucky enough to stumble upon a bubble sort by Richard Lowe.

This method is NOT the fastest and would not be the best solution for very large datasets, but if you are looking for an easy solution to sort a page or two of data, this is for you.

So, here is a real world example that is fairly easy to follow. I hope you find this useful.
Code:
'==========================================================================
'
' NAME: sort.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: http://www.thespidersparlor.com
' DATE  : 2/10/2004
'
' COMMENT: Reads a file into an array, sorts it and writes the data back to the file.
'
'==========================================================================


Option Explicit
Dim oFSO, ForReading, ForWriting, sortFile, MyList, myArray, ts, i, j, temp, line, report
ForReading = 1
ForWriting = 2
Set oFSO=CreateObject("Scripting.FileSystemObject")
[b][i]'comment out the next line if you want to supress prompting for the file location[/i][/b]
sortFile = InputBox("What file should I sort?  Full path please!", "File To Sort")
[b][i]'uncomment the next line if you want to have a static file location[/i][/b]
'sortFile = "C:\Test.log"
MyList= ofso.OpenTextFile(sortFile, ForReading).ReadAll
myArray=Split(MyList,vbCrLf, -1, vbtextcompare)
    
[i][b]'bubble sort thanks to Richard Lowe, 4GuysFromRolla.com [/b]
'what he does here is check each element in the array 
'against the next value to see if it is greater than it.
'If location1 is > location2 write location1 to temp,
'then write location2 to location1 and finally write
'temp to location2[/i]

for i = UBound(myArray) - 1 To 0 Step -1
    for j= 0 to i
        if myArray(j)>myArray(j+1) then
            temp=myArray(j+1)
            myArray(j+1)=myArray(j)
            myArray(j)=temp
        end if
    next
next 
[i]'end bubble sort.  Thanks Richard![/i]

For Each line In myArray
[b][i]	'Check for blank lines and ignore them[/i][/b]
	If Len(line) <> 0 Then
    report = report & line & vbcrlf
    End If
Next
   
MsgBox "The following will be written to " & sortfile & vbCrLf & report, vbOkOnly
  
[b][i]'Now write the data back to the original file in sorted order[/i][/b]
Set ts = oFSO.CreateTextFile (sortFile, ForWriting)
ts.write report
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top