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:"path\VBSort.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:"path\VBSort.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.