×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

VBScript FAQ

File and Data Processing

Easy to understand sorting by markdmac
Posted: 10 Feb 04 (Edited 13 Jun 12)

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")
'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
temp=myArray(j+1)
myArray(j+1)=myArray(j)
myArray(j)=temp
end if
next
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
End If
Next

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)
ts.write report

Back to VBScript FAQ Index
Back to VBScript Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close