Short answer: Your Dim testArray()
creates an abomination - a fixed array of no size that VBScript can't handle properly (just try to take its UBound()). Your script will work, if you delete those ().
Beginning of a long answer: Look at this sample code:
Option Explicit
Dim aDyn : aDyn = Split("Xylophone Elephant Ferret Ape Weasel")
WScript.Echo 0, " aDyn:", Join(aDyn)
Dim aDynCpy : aDynCpy = getSortedArray(aDyn)
WScript.Echo 1, "aDynCpy:", Join(aDynCpy)
WScript.Echo 2, " aDyn:", Join(aDyn)
Dim aFix(4) ' we want an optimized/non-growable fixed array
Dim i
For i = 0 To UBound(aFix)
aFix(i) = aDyn(i)
Next
WScript.Echo 3, " aFix:", Join(aFix)
SortArray aFix ' sort in place/reserved memory
WScript.Echo 4, " aFix:", Join(aFix)
aDynCpy = aDyn ' DYN: no problem
WScript.Echo 5, "aDynCpy:", Join(aDynCpy)
aDynCpy = getSortedArray(aFix) ' DYN: no problem
WScript.Echo 6, "aDynCpy:", Join(aDynCpy)
On Error Resume Next
aFix = aDyn ' FIX: not possible
WScript.Echo 7, Err.Description
On Error GoTo 0
WScript.Quit 0
Function getSortedArray(ByVal inArray) ' let VBScript do the copy
SortArray inArray ' sort copy in place
getSortedArray = inArray
End Function
Sub SortArray(inArray) ' ByRef/Default, because we sort in place
Dim a, b, swapVal
For a = UBound(inArray) - 1 To 0 Step -1
For b = 0 To a
If inArray(b) > inArray(b+1) Then
swapVal = inArray(b+1)
inArray(b+1) = inArray(b)
inArray(b) = swapVal
End If
Next
Next
End Sub
output:
cscript X6971815.vbs
0 aDyn: Xylophone Elephant Ferret Ape Weasel
1 aDynCpy: Ape Elephant Ferret Weasel Xylophone
2 aDyn: Xylophone Elephant Ferret Ape Weasel
3 aFix: Xylophone Elephant Ferret Ape Weasel
4 aFix: Ape Elephant Ferret Weasel Xylophone
5 aDynCpy: Xylophone Elephant Ferret Ape Weasel
6 aDynCpy: Ape Elephant Ferret Weasel Xylophone
7 Type mismatch
to get a feeling for how to deal with fixed/dynamic arrays wrt to copy vs. in-place modification. Fixed arrays - that can't be 'overwritten' - should be sorted in place/their reserved memory; all arrays can be copied into a dynamic one.
Masochists may look at this disgusting discussion