I'm writing a VBScript function, which takes as in put an array of strings and returns an array of the same strings sorted alphabetically. Ideally, I'd like this function to work exactly like (as an example) the uCase function, where you can say:
myString = ucase(mystring)
Where the value of "myString" will get replaced with the uppercase version of its contents.
But when I set up an array in my test script, then feed it to my sorting function, I get a type mismatch error assigning the result to the original variable. All I can figure is that VBScript must not allow you to overwrite an array with another array, because the TypeName function tells me that both the input variable I'm using and the output from the sorting function are the same type ("Variant()").
Here's my example code to give you something more concrete to work with:
Option Explicit
Dim testArray(), thisItem, sortedArray
ReDim testArray(9)
testArray(0)="Xylophone"
testArray(1)="Elephant"
testArray(2)="Ferret"
testArray(3)="Eel"
testArray(4)="Dinosaur"
testArray(5)="Barracuda"
testArray(6)="Ape"
testArray(7)="Weasel"
testArray(8)="Firebird"
testArray(9)="Phoenix"
WScript.Echo "Starting Array:"
For Each thisItem In testArray
WScript.Echo " " & thisItem
Next
' This line will work fine, assigning Variant() to Empty sortedArray
sortedArray = SortArray(testArray)
' This line will generate a type mismatch, assigning Variant() to Variant()
testArray = SortArray(testArray)
WScript.Echo "Sorted Array:"
For Each thisItem In sortedArray
WScript.Echo " " & thisItem
Next
WScript.Quit 0
Function SortArray(ByVal inArray)
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
SortArray = inArray
End Function
If you run this example code as-is, you'll find that the line beginning with "sortedArray=" will work just fine. The line below that, beginning with "testArray=" will generate a type mismatch error.
Ideally, I'd like this function to operate such that EITHER or BOTH of those lines would work properly. (In other words, when I use it later I don't want to have to think "Oh yeah, I have to create another variable and assign the output to that instead of my original variable.")
Am I missing something or is VBScript incapable of doing what I want it to do here?