Subscribe to Windows IT Pro
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

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.

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

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