Rob van der Woude's Scripting Pages

VBScript Scripting Techniques > Data > Arrays

Arrays

  1. Sort
    Case insensitive bubble sort subroutine
  2. Display
    Display the entire array's content on screen
  3. Reverse
    Reverse the order of array elements

 

Sort

Bubble sort algorithm borrowed from the Scripting Guys.

Case insensitive sorting subroutine.

Usage:

Sort array_name

VBScript code:

Sub Sort( ByRef myArray )
	Dim i, j, strHolder

	For i = ( UBound( myArray ) - 1 ) to 0 Step -1
		For j= 0 to i
			If UCase( myArray( j ) ) > UCase( myArray( j + 1 ) ) Then
				strHolder        = myArray( j + 1 )
				myArray( j + 1 ) = myArray( j )
				myArray( j )     = strHolder
			End If
		Next
	Next 
End Sub

 

Display

List the contents of an array on screen.

VBScript code:

WScript.Echo Join( myArray, vbCrLf )

 

Reverse

Subroutine to reverse the order of array elements.

Usage:

Reverse array_name

VBScript code:

Sub Reverse( ByRef myArray )
	Dim i, j, idxLast, idxHalf, strHolder

	idxLast = UBound( myArray )
	idxHalf = Int( idxLast / 2 )

	For i = 0 To idxHalf
		strHolder              = myArray( i )
		myArray( i )           = myArray( idxLast - i )
		myArray( idxLast - i ) = strHolder
	Next
End Sub

page last uploaded: 2016-09-19, 14:58