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.
' 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.
Dim oFSO, ForReading, ForWriting, sortFile, MyList, myArray, ts, i, j, temp, line, report
ForReading = 1
ForWriting = 2
Set oFSO=CreateObject("Scripting.FileSystemObject") 'comment out the next line if you want to supress prompting for the file location
sortFile = InputBox("What file should I sort? Full path please!", "File To Sort") 'uncomment the next line if you want to have a static file location
'sortFile = "C:\Test.log"
MyList= ofso.OpenTextFile(sortFile, ForReading).ReadAll
myArray=Split(MyList,vbCrLf, -1, vbtextcompare)
'bubble sort thanks to Richard Lowe, 4GuysFromRolla.com
'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
for i = UBound(myArray) - 1 To 0 Step -1
for j= 0 to i
if myArray(j)>myArray(j+1) then
next 'end bubble sort. Thanks Richard!
For Each line In myArray 'Check for blank lines and ignore them
If Len(line) <> 0 Then
report = report & line & vbcrlf
MsgBox "The following will be written to " & sortfile & vbCrLf & report, vbOkOnly
'Now write the data back to the original file in sorted order
Set ts = oFSO.CreateTextFile (sortFile, ForWriting)