More Really Useful VBScript Array Functions – ArraySort and DoubleArraySort

A while back I wrote an article about VBScript arrays and their various shortcomings entitled “VBScript Array Functions – DeleteFromArray, InArray”. I love these functions because they really simplify the manipulation of arrays and directly address some of the obvious flaws with the initial implementation of arrays in VBScript.

At the end of that article, written almost a year ago, I also mention writing sort functions for VBScript. Well I finally go around to it today and decided that these two functions were just way to handy not to share.

The first one, which will probably be the one most commonly used is a simple sort function, where you can sort ascendingly or descendingly depending on which value you pass for sOrder. The nice thing about this, is that it will sort your array simply by calling it as a normal sub.

The second sub works exactly like the first one, except that it will sort 2 arrays relationally. For example, lets say you have Array1 and Array2 who’s indexes are related. Lets say now you wish to sort Array1 but also maintain the 1:1 relationship you have between Array1 and Array2. This sub will do exactly that by passing Array1 as the first parameter, Array2 as the second parameter and the order, descending or ascending as the final parameter.

I think these are pretty slick and many of you will find them immensely useful in your own scripts and projects. As usual, I’d love to get any feedback so feel free to post below!

Sub ArraySort(aArrayToSort, sOrder)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com
  'This Sub will sort the array passed as aArrayToSort
  For i = UBound(aArrayToSort) - 1 To 0 Step -1
    For j = 0 To i
      If aArrayToSort(j) < aArrayToSort(j+1) And sOrder = "desc" Then
        sTempStr = aArrayToSort(j+1)
        aArrayToSort(j+1) = aArrayToSort(j)
        aArrayToSort(j) = sTempStr
      ElseIf aArrayToSort(j) > aArrayToSort(j+1) And sOrder = "asc" Then
        sTempStr = aArrayToSort(j+1)
        aArrayToSort(j+1) = aArrayToSort(j)
        aArrayToSort(j) = sTempStr
      End If
    Next
  Next
End Sub

Sub DoubleArraySort(aArray1ToSort, aArray2ToSort, sOrder)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com
  'This Sub will sort the array passed as aArray1ToSort,
  'the values in aArray2ToSort, will not be sorted, but
  'will be reordered in the same relational order as
  'aArray1ToSort
  For i = UBound(aArray1ToSort) - 1 To 0 Step -1
    For j = 0 To i
      If aArray1ToSort(j) > aArray1ToSort(j+1) And sOrder = "desc" Then
        sTempStr = aArray1ToSort(j+1)
        aArray1ToSort(j+1) = aArray1ToSort(j)
        aArray1ToSort(j) = sTempStr
        
        sTempStr = aArray2ToSort(j+1)
        aArray2ToSort(j+1) = aArray2ToSort(j)
        aArray2ToSort(j) = sTempStr
      ElseIf aArray1ToSort(j) < aArray1ToSort(j+1) And sOrder = "asc" Then
        sTempStr = aArray1ToSort(j+1)
        aArray1ToSort(j+1) = aArray1ToSort(j)
        aArray1ToSort(j) = sTempStr
        
        sTempStr = aArray2ToSort(j+1)
        aArray2ToSort(j+1) = aArray2ToSort(j)
        aArray2ToSort(j) = sTempStr
      End If
    Next
  Next 
End Sub

 

Leave a Comment