28Jul/069
VBScript: Sort Array
Found this code and wanted to bookmark it... from VisualBasicScript.com's Forums
Function fSortArray(aSortThisArray)
Set oArrayList = CreateObject("System.Collections.ArrayList" )
For iElement = 0 To UBound(aSortThisArray)
oArrayList.Add aSortThisArray(iElement)
Next
oArrayList.Sort
set fSortArray = oArrayList
Set oArrayList = Nothing
End Function



August 17th, 2006 - 10:12
Just FYI this only works if you are useing a one d array
September 1st, 2006 - 08:48
Dear Chrissy,
Thanks a lot for this tip.
)
I still use a standard sort code
Now, my code will be more simple
MERCI
Arnaud
“French Wine is the blood of France”
October 4th, 2006 - 08:27
Hey, kinda neat to search google, and find my code somewhere else! lol in any case, jsut watned to leave a warning that the System.Collections.ArrayList object can only be created if you .NET installed. On systems that haven’t had that installed, it will error-out with a “Microsoft VBScript runtime error: ActiveX component can’t create object: ‘System.Collections.ArrayList’” error.
Have a good one!
DS
January 31st, 2007 - 01:06
Thanks, works perfect with Rational Clear Quest basic scripts.
February 1st, 2007 - 17:41
you can drop the dictionary, just using memory…never used
April 4th, 2008 - 12:06
I need to check the sorting of a table based on the column headers of a web table. Also, i can sort one column in ascending while the other column being in descending.
Any solutions?
February 20th, 2009 - 16:33
This function does not return sorted array, it returns an object!
You can not use it like MyArray = fSortArray(MyArray)
To return array you need something like:
redim res(Ubound(aSortThisArray))
i = 0
for each x in oArrayList
res(i) = x
i = i+1
next
SortArray = res
March 26th, 2009 - 08:55
No you don’t, you only need to do something like:
Set sortedArray = fSortArray(unsortedArray)
November 20th, 2009 - 18:55
Yes, you do, Ryan!
In your example sortedArray is an OBJECT, not an ARRAY.
“set” clearly indicates assignment to an OBJECT.
The whole point of my post was that sorting ARRAY you should get sorted ARRAY, not an OBJECT. I mean, correct sort-array function should return variable of the same type as its argument, i.e. ARRAY, not an OBJECT.
In hope you can understand the difference between ARRAY and OBJECT,
Alexei