How do I sort arrays using vbscript?

Oskar picture Oskar · Nov 6, 2008 · Viewed 114.9k times · Source

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

Answer

Oskar picture Oskar · Nov 6, 2008

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