nerds:~ #

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
Posted by: Chrissy   Filed under: VBScript Leave a comment
Comments (9) Trackbacks (0)
  1. Just FYI this only works if you are useing a one d array

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

  3. 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

  4. Thanks, works perfect with Rational Clear Quest basic scripts.

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

  6. 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. 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

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

  9. 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


Leave a comment


No trackbacks yet.