QTP Math functions – TypedArraySort
Parent page: Service Functions – Math (QTP, VBScript)
Description
Sorts one-dimentional array in ascending / descending order with typecasting.
Implementation
Public Function TypedArraySort(ByRef dvArray, ByVal sFormat, ByVal boolAscending) Dim Iter, Jter, aValue, boolRC If Not isArray(dvArray) Then dvArray = Array(dvArray) ArraySort = dvArray End If sFormat = UCase(sFormat) For Iter = 0 To UBound(dvArray) Select Case sFormat Case "NUMBER" dvArray(Iter)= DblVal(dvArray(Iter)) Case "DATE" dvArray(Iter)= DateVal(dvArray(Iter)) Case "STRING" dvArray(Iter)= CStr(dvArray(Iter)) Case Else dvArray(Iter)= DblVal(dvArray(Iter)) sFormat = "NUMBER" End Select Next For Jter = 0 To UBound(dvArray) For Iter = 0 To UBound(dvArray)-1 aValue = dvArray(Iter) Select Case sFormat Case "NUMBER" boolRC = aValue > dvArray(Iter+1) Case "DATE" boolRC = DateDiff("s", aValue, dvArray(Iter+1)) < 0 Case "STRING" boolRC = StrComp(aValue, dvArray(Iter+1)) = 1 End Select If boolRC 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 TypedArraySort = dvArray End Function
Unit Tests
dvArray = Array(2,3,5,1,9,-1) Call TypedArraySort(dvArray, "Number", False) If dvArray(5) <> -1 Then Log.Error("TypedArraySort failed") End If If dvArray(0) <> 9 Then Log.Error("TypedArraySort failed") End If If dvArray(3) <> 2 Then Log.Error("TypedArraySort failed") End If dvArray = Array(2,"3.0",5,"1.00",9,-1, "a") Call TypedArraySort(dvArray, "Number", True) If dvArray(6) <> 9 Then Log.Error("TypedArraySort failed") End If If dvArray(0) <> -1 Then Log.Error("TypedArraySort failed") End If If dvArray(3) <> 2 Then Log.Error("TypedArraySort failed") End If dvArray = Array(2,"3.0",5,"1.00",9,-1, "a", "-10") Call TypedArraySort(dvArray, "String", True) If dvArray(7) <> "a" Then Log.Error("TypedArraySort failed") End If If dvArray(6) <> "9" Then Log.Error("TypedArraySort failed") End If If dvArray(0) <> "-1" Then Log.Error("TypedArraySort failed") End If If dvArray(3) <> "2" Then Log.Error("TypedArraySort failed") End If dvArray = Array("20/06/1900","01/01/01","31/12/2050","20/20/20") Call TypedArraySort(dvArray, "Date", False) If dvArray(0) <> CDate("31/12/2050") Then Log.Error("TypedArraySort failed") End If