Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

March 13, 2006 12:00 AM

Sorting Arrays in VBScript

Implementing the Penton.VBSort object
Windows IT Pro
InstantDoc ID #49458
Rating: (1)
Downloads
49458.zip

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.

Related Content:

ARTICLE TOOLS

Comments
  • ibrahim
    6 years ago
    Apr 01, 2006

    Good

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.