views:

6693

answers:

7

The question says it all really, but...

I'm scanning through a file looking for lines that match a certain regex pattern, and then I want to print out the lines that match but in alphabetical order. I'm sure this is trivial but vbscript isn't my background

my array is defined as

Dim lines(10000)

if that makes any difference, and I'm trying to execute my script from a normal cmd prompt

thanks

+5  A: 

From microsoft

Sorting arrays in VBScript has never been easy; that’s because VBScript doesn’t have a sort command of any kind. In turn, that always meant that VBScript scripters were forced to write their own sort routines, be that a bubble sort routine, a heap sort, a quicksort, or some other type of sorting algorithm.

So (using .Net as it is installed on my pc):

Set outputLines = CreateObject("System.Collections.ArrayList")

'add lines
outputLines.Add output
outputLines.Add output

outputLines.Sort()
For Each outputLine in outputLines
    stdout.WriteLine outputLine
Next
Oskar
A: 

You either have to write your own sort by hand, or maybe try this technique:

http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=83

You can freely intermix server side javascript with VBScript, so wherever VBScript falls short, switch to javascript.

Corey Trager
I get "Microsoft VBScript compilation error: Expected statement" so I suppose I can't just run the vb script from the command line?
Oskar
Oops, sorry. I guess I was thinking you are working in a classic ASP page. Could you explain more what you are doing? If you have the choice, something like python is VERY VERY EASY to install and learn. You might find you'll have it done sooner in python even starting at zero.
Corey Trager
I agree, anything else would be easier, but the script was created by someone else ... Still, I updated my answer above with how to do it (since I have .Net on my pc and can cheat)
Oskar
+1  A: 

VBScript does not have a method for sorting arrays so you've got two options:

  • Writing a sorting function like mergesort, from ground up.
  • Use the JScript tip from this article
Gabe
A: 

I actually just had to do something similar but with a 2D array yesterday. I am not that up to speed on vbscript and this process really bogged me down. I found that the articles here were very well written and got me on the road to sorting in vbscript.

wergeld
A: 

Here is a QuickSort that I wrote for the arrays returned from the GetRows method of ADODB.Recordset.

'Author:  Eric Weilnau
'Date Written: 7/16/2003
'Description: QuickSortDataArray sorts a data array using the QuickSort algorithm.
'    Its arguments are the data array to be sorted, the low and high
'    bound of the data array, the integer index of the column by which the
'    data array should be sorted, and the string "asc" or "desc" for the
'    sort order.
'
Sub QuickSortDataArray(dataArray, loBound, hiBound, sortField, sortOrder)
 Dim pivot(), loSwap, hiSwap, count
 ReDim pivot(UBound(dataArray))

 If hiBound - loBound = 1 Then
  If (sortOrder = "asc" and dataArray(sortField,loBound) > dataArray(sortField,hiBound)) or (sortOrder = "desc" and dataArray(sortField,loBound) < dataArray(sortField,hiBound)) Then
   Call SwapDataRows(dataArray, hiBound, loBound)
  End If
 End If

 For count = 0 to UBound(dataArray)
  pivot(count) = dataArray(count,int((loBound + hiBound) / 2))
  dataArray(count,int((loBound + hiBound) / 2)) = dataArray(count,loBound)
  dataArray(count,loBound) = pivot(count)
 Next

 loSwap = loBound + 1
 hiSwap = hiBound

 Do
  Do While (sortOrder = "asc" and dataArray(sortField,loSwap) <= pivot(sortField)) or sortOrder = "desc" and (dataArray(sortField,loSwap) >= pivot(sortField))
   loSwap = loSwap + 1

   If loSwap > hiSwap Then
    Exit Do
   End If
  Loop

  Do While (sortOrder = "asc" and dataArray(sortField,hiSwap) > pivot(sortField)) or (sortOrder = "desc" and dataArray(sortField,hiSwap) < pivot(sortField))
   hiSwap = hiSwap - 1
  Loop

  If loSwap < hiSwap Then
   Call SwapDataRows(dataArray,loSwap,hiSwap)
  End If
 Loop While loSwap < hiSwap

 For count = 0 to Ubound(dataArray)
  dataArray(count,loBound) = dataArray(count,hiSwap)
  dataArray(count,hiSwap) = pivot(count)
 Next

 If loBound < (hiSwap - 1) Then
  Call QuickSortDataArray(dataArray, loBound, hiSwap-1, sortField, sortOrder)
 End If

 If (hiSwap + 1) < hiBound Then
  Call QuickSortDataArray(dataArray, hiSwap+1, hiBound, sortField, sortOrder)
 End If
End Sub
Eric Weilnau
A: 

its dosent work for me... what are athe arguments can u please post an example

+2  A: 

Disconnected recordsets can be useful.

Const adVarChar = 200  'the SQL datatype is varchar

'Create a disconnected recordset
Set rs = CreateObject("ADODB.RECORDSET")
rs.Fields.append "SortField", adVarChar, 25

rs.CursorType = adOpenStatic
rs.Open
rs.AddNew "SortField", "Some data"
rs.Update
rs.AddNew "SortField", "All data"
rs.Update

rs.Sort = "SortField"

rs.MoveFirst

Do Until rs.EOF
    strList=strList & vbCrLf & rs.Fields("SortField")        
    rs.MoveNext
Loop 

MsgBox strList
Remou
I should have used http://www.w3schools.com/ADO/met_rs_getstring.asp
Remou