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)
set fSortArray = oArrayList
Set oArrayList = Nothing
End Function

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, holds a master's degree in Systems Engineering and is coauthor of Learn dbatools in a Month of Lunches. Chrissy is certified in SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in VBScript
12 comments on “VBScript: Sort Array
  1. Arnaud says:

    Dear Chrissy,

    Thanks a lot for this tip.
    I still use a standard sort code :(
    Now, my code will be more simple ;))


    “French Wine is the blood of France”

  2. 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!

  3. Chrissy says:

    AD should automatically be finding it for you. The above workaround is really a hack. Check your Active Directory Sites and Services to ensure that you've got sites setup and working properly.

  4. Giannis says:

    Thanks, works perfect with Rational Clear Quest basic scripts.

  5. not required says:

    you can drop the dictionary, just using memory…never used

  6. abhishek says:

    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?

  7. Alexei says:

    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
    SortArray = res

  8. Ryan says:

    No you don’t, you only need to do something like:
    Set sortedArray = fSortArray(unsortedArray)

  9. Alexei says:

    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,

  10. Yo can also Set a variant, which is an object or anything else.

    Just start your function with

    Function fSortArray(aSortThisArray) As Variant


    set fSortArray = oArrayList

    will return your array.

  11. James says:

    I have imported into an array a comma delimited text file with 5 fields. How do I use the above sort function to sort on the first field?

  12. TSolina says:

    Or even simplier you can add parameter to function and back sorted elements to first array.

    Function fSortArray(aSortThisArray, retVal)
    Dim oArrayList, i, arLen
    Set oArrayList = CreateObject( “System.Collections.ArrayList” )
    arLen = UBound(aSortThisArray)
    For i = 0 To arLen
    oArrayList.Add aSortThisArray(i)

    For i = 0 to arLen
    aSortThisArray(i) = oArrayList(i)

    retVal = aSortThisArray
    Set oArrayList = Nothing
    End Function

    and calling

    call fSortArray(myArray, myArray)

Leave a Reply

Your email address will not be published. Required fields are marked *