udfArrayBinSort
arr udfArrayBinSort (arr, int)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfArrayBinSort (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
intLenMax = 0
For intI = intLow To intHigh
   intLenMax = Max (intLenMax, StrLen (arrArray [intI]))
Next
hdlBB = BinaryAlloc (intLenMax * intElements)
For intI = intLow To intHigh
   BinaryPokeStr (hdlBB, intI * intLenMax, arrArray [intI])
Next
BinarySort (hdlBB, intLenMax, 0, intLenMax, @STRING | intDirection)
For intI = intLow To intHigh
   arrArray [intI] = BinaryPeekStr (hdlBB, intI * intLenMax, intLenMax)
Next
hdlBB = BinaryFree (hdlBB)
Return arrArray
;..........................................................................................................................................
; Sort parameter:
; intDirection=@ASCENDING
; intDirection=@DESCENDING
;
; Detlev Dalitz.20010709.20090513.
;..........................................................................................................................................
#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: udfBinSort (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 = udfArrayBinSort (arrB, @ASCENDING)
arrC = udfArrayBinSort (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