;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfArrayItemSort (arrArray, intDirection) If intDirection != @ASCENDING && intDirection != @DESCENDING Then intDirection = @ASCENDING If !ArrInfo (arrArray, -1) Then Return ArrDimension (0) ; Invalid input array, return empty valid dim-0 array. If ArrInfo (arrArray, 0) != 1 Then Return ArrDimension (0) ; Only dim-1 array allowed, return empty valid dim-0 array. intElements = ArrInfo (arrArray, 1) If intElements == 0 Then Return ArrDimension (0) ; Input array has no elements, return empty valid dim-0 array. If intElements == 1 Then Return arrArray ; Input array has only one element, return the input array. intHigh = intElements - 1 intLow = 0 strDelimiter = Num2Char (7) ; Assuming ASCII-7 bell character does not exist in data! strList = "" For intI = intLow To intHigh strList = strList : strDelimiter : arrArray [intI] Next strList = ItemSort (StrSub (strList, 2, -1), strDelimiter) Switch intDirection Case @ASCENDING Drop (arrArray) arrArray = Arrayize (strList, strDelimiter) Break Case @DESCENDING For intI = intLow To intHigh arrArray [intI] = ItemExtract (intElements - intI, strList, strDelimiter) Next Break EndSwitch Return arrArray ;.......................................................................................................................................... ; Sort parameter: ; intDirection=@ASCENDING ; intDirection=@DESCENDING ;.......................................................................................................................................... ; Note: ; Using code fragment (B) instead of fragment (A) for writing sorted items back to array ; tunes up the performance speed of the ascending sort implementation of about 30 Pct.! ; (A) ; For i=intLow To intHigh ; arrArray [i] = ItemExtract(i+1,sSort,strDelimiter) ; Next ; (B) ; Drop(arrArray) ; arrArray = Arrayize(sSort,strDelimiter) ;.......................................................................................................................................... #EndFunction ;------------------------------------------------------------------------------------------------------------------------------------------ ;---------------------------------------------------------------------------------------------------------------------- #DefineFunction udfArrayItemize (arrArray, strDelimiter) If !ArrInfo (arrArray, -1) Then Return "" ; No array. If !ArrInfo (arrArray, 6) Then Return "" ; No elements. If ArrInfo (arrArray, 0) > 1 Then Return "" ; Too much dimensions. strItemList = "" intHigh = Max (ArrInfo (arrArray, 1) - 1, 0) intLow = 0 For intElem = intLow To intHigh If !!VarType (arrArray [intElem]) strItemList = ItemInsert (arrArray [intElem], -1, strItemList, strDelimiter) Else strItemList = ItemInsert ("", -1, strItemList, strDelimiter) EndIf Next Return strItemList ;---------------------------------------------------------------------------------------------------------------------- ; This UDF "udfArrayItemize" converts a given dim-1 array into an itemlist ; with each item separated by delimiter character. ; ; Example: strMyItemList = udfArrayItemize (arrMyArray, @TAB) ; Creates an itemList from array. ; ; Note: ; This UDF supports only dim-1 array. ; An array element which is not initialized has a Vartype=0 (undefined). ; Therefore an empty item will be appended to target itemlist. ; ; Detlev Dalitz.20020718.20090513. ;---------------------------------------------------------------------------------------------------------------------- #EndFunction ;---------------------------------------------------------------------------------------------------------------------- ; Test. strMsgTitle = "Demo: udfArrayItemSort (arrArray, intDirection)" strMsgText = "" strDelimiter = "," strItemList = "22,q,w,e,r,t,T,999,a,s,d,f,1,/z,B5,-196" ; 16 Items. arrA = Arrayize (strItemList, strDelimiter) arrB = Arrayize (strItemList, strDelimiter) arrC = Arrayize (strItemList, strDelimiter) arrB = udfArrayItemSort (arrB, @ASCENDING) arrC = udfArrayItemSort (arrC, @DESCENDING) strMsgText = strMsgText : "Array A: " : @LF : udfArrayItemize (arrA, strDelimiter) : @LF : @LF ; "22,q,w,e,r,t,T,999,a,s,d,f,1,/z,B5,-196" strMsgText = strMsgText : "Array B sorted ascending: " : @LF : udfArrayItemize (arrB, strDelimiter) : @LF : @LF ; "/z,1,-196,22,999,a,B5,d,e,f,q,r,s,t,T,w" ==> "1,-196" vs. "-196,1" !!! strMsgText = strMsgText : "Array C sorted descending: " : @LF : udfArrayItemize (arrC, strDelimiter) : @LF : @LF ; "w,T,t,s,r,q,f,e,d,B5,a,999,22,-196,1,/z" ==> "-196,1" vs. "1,-196" !!! Message (strMsgTitle, strMsgText) Exit