QTP Math functions - ArraySort

Posted by Albert Gareev on Jan 12, 2008 | Categories: MathSource code

Parent page: Service Functions – Math (QTP, VBScript)

Description

Sorts one-dimentional array in ascending / descending order.

Implementation

Public Function ArraySort(ByRef dvArray, ByVal boolAscending)
  Dim Iter, Jter, aValue
  If Not isArray(dvArray) Then
    dvArray = Array(dvArray)
    ArraySort = dvArray
  End If
 For Jter = 0 To UBound(dvArray)
  For Iter = 0 To UBound(dvArray)-1
   aValue = dvArray(Iter)
   If aValue > dvArray(Iter+1) Then
    If boolAscending Then
     dvArray(Iter) = dvArray(Iter+1)
     dvArray(Iter+1) = aValue
    Else
     'do nothing
    End If
   Else
    If boolAscending Then
     'do nothing
    Else
     dvArray(Iter) = dvArray(Iter+1)
     dvArray(Iter+1) = aValue
    End If
   End If
  Next
 Next
 ArraySort = dvArray
End Function

Unit Tests

  Log.Message("Array routines")
  dvArray = Array(2,3,5,1,9,-1)
  Call ArraySort(dvArray, True)
  If dvArray(0) <> -1 Then
    Log.Error("ArraySort failed")
  End If
  If dvArray(5) <> 9 Then
    Log.Error("ArraySort failed")
  End If
  If dvArray(3) <> 3 Then
    Log.Error("ArraySort failed")
  End If
  dvArray = 1
  Call ArraySort(dvArray, True)
  If dvArray(0) <> 1 Then
    Log.Error("ArraySort failed")
  End If
  dvArray = ""
  Call ArraySort(dvArray, True)
  If dvArray(0) <> "" Then
    Log.Error("ArraySort failed")
  End If
  dvArray = ArraySort(sEmpty, True)
  If dvArray(0) <> "" Then
    Log.Error("ArraySort failed")
  End If
  dvArray = Array(2,3,5,1,9,-1)
  Call ArraySort(dvArray, False)
  If dvArray(5) <> -1 Then
    Log.Error("ArraySort failed")
  End If
  If dvArray(0) <> 9 Then
    Log.Error("ArraySort failed")
  End If
  If dvArray(3) <> 2 Then
    Log.Error("ArraySort failed")
  End If

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.