Sorting Arrays in VBScript

Implementing the Penton.VBSort object

Bill Stewart

March 12, 2006

5 Min Read
ITPro Today logo in a gray background | ITPro Today

 

Sorting an array is a common scripting task. The task is so common, in fact, that JScript (Microsoft's version of JavaScript) includes a sort method that sorts an array. VBScript, however, lacks the native capability to sort an array. I've written a Windows script component, VBSort.wsc, to address this limitation. Listing 1 shows VBSort.wsc. The Penton.VBSort object, implemented in the VBSort.wsc file, provides a set of three properties and a method that lets you quickly sort an array in VBScript.

 

Registering the Component
As with all script components, you must register VBSort.wsc on your computer before you use it in your scripts. To perform the registration, right-click VBSort .wsc in Windows Explorer and choose Register. (Conversely, to unregister the component, right-click VBSort.wsc and choose Unregister.) To register the component from a command prompt, use the following command:

regsvr32 /i:"pathVBSort.wsc"  %SystemRoot%system32  scrobj.dll 

where path is the directory in which theVBSort.wsc file resides.

To unregister the component from the command prompt, use the command

regsvr32 /u /n   /i:"pathVBSort.wsc"  %SystemRoot%system32  scrobj.dll 

If you want to run either command silently, specify /s as the first option on the command line after the Regsvr32 command, as follows:

regsvr32 /s 

Note that to register or unregister a component, you must use an account that's a member of the computer's local Administrators group.

Deploying the Penton.VBSort Object
After you've registered the component, you're ready to use the Penton.VBSort object in your own scripts. You can create an instance of the object in VBScript using the following line of code:

Set VBSort =   CreateObject("Penton.VBSort") 

Table 1 lists the three properties that control the object's sorting behavior. All three properties are set to write-only Boolean values (i.e., True or False), with a default value of False. For example, to set the IgnoreCase property, you would use the following line of VBScript code:

VBSort.IgnoreCase = True 

To sort an array, you call the object's Sort method with the array variable's name as an argument. For example, to sort the array that the variable MyArray references, you would use the VBScript code

VBSort.Sort MyArray 

If you want to use any of the object's properties, you must set them before you execute the Sort method. If you don't set any of the properties, the Sort method performs a case-sensitive ascending sort. Also, the Sort method doesn't make a copy of the array being sorted; it sorts the array in place.

Inside the Penton.VBSort Object
The Penton.VBSort object is implemented in theVBSort.wsc file. Windows Script Components (WSC) files are XML-formatted text files that implement a COM component in script code. For information about the XML tags that you need to create a script component, see my article "WSH, Part 3: Windows Script Components," March 2006, InstantDoc ID 49092.

The beginning of the file defines the object's interface (i.e., the properties and methods available to calling programs). The object uses two global variables that determine the sorting behavior, as callout A in Listing 1 shows. The SortFlags variable stores a numeric bitmap that determines which function will compare array elements. CompareFunc contains a reference to the comparison function. When the property procedures (put_Descending, put_IgnoreCase, and put_Random) execute, they set or clear the corresponding bit in Sort-Flags, depending on whether the parameter value declared to the function is True or False.

The Sort subroutine checks the bit value in the SortFlags variable and uses VBScript's GetRef function to assign the appropriate sort comparison function. The GetRef function returns a reference to a function or a subroutine. The Sort function then executes the Quicksort subroutine to sort the array, as the code in callout B shows.

The Quicksort Subroutine
Callout C shows the Quicksort subroutine, which requires three parameters: the name of the array to sort and the lower and upper indexes indicating the range of array elements to be sorted. To sort the entire array, the Sort subroutine calls the Quicksort subroutine with a lower index of the first element (i.e., zero) and an upper index of the last element (determined by the UBound function).

Quicksort is a divide-and-conquer sorting algorithm that quickly divides the array at the midpoint, sorts the portions of the array before and after the midpoint, and continues recursively until the entire array is sorted. (You can find a discussion of how the Quicksort algorithm works in most computer science textbooks.)

Comparing Array Elements
To allow for different ordering of array elements, the Quicksort subroutine uses the function reference in the CompareFunc variable to compare array elements. All of the comparison functions require two arguments containing the array items being compared. If the first item is less than the second, the comparison function returns -1; if the items are equal, the function returns 0; and if the first item is greater than the second, the function returns 1.

The comparison functions first use VBScript's IsNumeric function to determine whether the items being compared are numeric. If both items are numeric, the function will use the formula (item1 rather than the

If the items being compared aren't numeric, the comparison functions use VBScript's StrComp function because it's faster than the operators for comparing strings ( particularly when case is ignored). For example, the comparison expression

StrComp(item1, item2, vbTextCompare) 

is much faster than the equivalent

(LCase(item1)   

The first expression is faster than the second because it optimizes the sorting process, particularly for large arrays. The second expression executes more slowly because it calls the LCase function four times.

I chose the return values of -1, 0, and 1 because this set of values is the same set that StrComp returns, making the code relatively easy to read. (In addition, JScript and JavaScript require the same return values to define a sort comparison function.)

Sorting Randomly
If you set the Penton.VBSort object's Random property to True, the object will use the SortRandom function to compare array elements. The Sort-Random function uses VBScript's Rnd function to randomly choose a return value of -1, 0, or 1. The Sort subroutine uses the Randomize statement to initialize the random number generator before calling the Quicksort subroutine. I added this capability because it was useful in testing the object and simple to implement. (Although the random sorting feature is limited in scope, I see many requests on Usenet for an "unsort" function.)

Sorting Made Simple
The Penton.VBSort object effectively addresses a limitation in the VBScript language. In an upcoming article, I'll present a script that uses this object to create a useful command-line sorting utility.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like