udfItemListSortDesc
udfItemListSortNum
udfItemListSortFlt
udfItemListSortByteComp
str udfItemListSortDesc (str, str)
str udfItemListSortDescV2 (str, str)
str udfItemListSortNum (str, str, int)
str udfItemListSortNumV2 (str, str, int)
str udfItemListSortFlt (str, str, int)
str udfItemListSortByteComp (str, str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListSortDesc (strItemList, strDelimiter)
If strItemList == "" Then Return ""
strAscList = ItemSort (strItemList, strDelimiter)
intCount = ItemCount (strAscList, strDelimiter)
strDescList = ""
For intItem = intCount To 1 By -1
   strDescList = ItemInsert (ItemExtract (intItem, strAscList, strDelimiter), -1, strDescList, strDelimiter)
Next
Return strDescList
;..........................................................................................................................................
; This UDF "udfItemListSortDesc" sorts an itemlist of alphanumerical values in descending direction.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListSortDescV2 (strItemList, strDelimiter) ; Different algorithm than "udfItemListSortDesc".
If strItemList == "" Then Return ""
strItemList = ItemSort (strItemList, strDelimiter)
intCount = ItemCount (strItemList, strDelimiter)
For intItem = intCount To 1 By -1
   strItemList = ItemRemove (intItem, ItemInsert (ItemExtract (intItem, strItemList, strDelimiter), -1, strItemList, strDelimiter), strDelimiter)
Next
Return strItemList
;..........................................................................................................................................
; This UDF "udfItemListSortDescV2" sorts an itemlist of alphanumerical values in descending direction.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListSortNum (strItemList, strDelimiter, intDirection)
If strItemList == "" Then Return ""
strDelimiterSave = strDelimiter
strDelimiter = Num2Char (1)
strItemList = StrReplace (strItemList, strDelimiterSave, strDelimiter)
intCount = ItemCount (strItemList, strDelimiter)
strTempList = ""
For intElem = 1 To intCount
   strItem = ItemExtract (intElem, strItemList, strDelimiter)
   strItem = StrFixLeft (strItem, "", 16)
   strTempList = ItemInsert (strItem, -1, strTempList, strDelimiter)
Next
strTempList = ItemSort (strTempList, strDelimiter)
strSortList = ""
intBoundGT = -1 * (intDirection == @ASCENDING)
intBoundLT = -1 * (intDirection == @DESCENDING)
For intItem = 1 To intCount
   strItem = StrTrim (ItemExtract (intItem, strTempList, strDelimiter))
   If strItem >= 0 Then strSortList = ItemInsert (strItem, intBoundGT, strSortList, strDelimiter)
      Else strSortList = ItemInsert (strItem, intBoundLT, strSortList, strDelimiter)
Next
strSortList = StrReplace (strSortList, strDelimiter, strDelimiterSave)
Return strSortList
;..........................................................................................................................................
; This UDF "udfItemListSortNum" sorts an itemlist of integer numbers in ascending or descending direction.
; The return value is an itemlist of sorted integer numbers.
;
; Example:
; strItemList = "0,1,-3,5,-7,9,-11,13,15,2,-4,6,-8,10,-12,14"
;
; intDirection = @ASCENDING
; strSortList = "-12,-11,-8,-7,-4,-3,0,1,2,5,6,9,10,13,14,15"
;
; intDirection = @DESCENDING
; strSortList = "15,14,13,10,9,6,5,2,1,0,-3,-4,-7,-8,-11,-12"
;
; Note: Blank char is allowed to use as itemlist delimiter.
;
; Detlev Dalitz.20020915.20090430.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListSortNumV2 (strItemList, strDelimiter, intDirection)
If strItemList == "" Then Return ""
strComparer = ">"
If intDirection == @DESCENDING Then strComparer = "<"
arrArray = Arrayize (strItemList, strDelimiter)
intKHigh = Max (0, ArrInfo (arrArray, 1) - 1)
intIHigh = intKHigh - 1
intILow = 0
For intI = intILow To intIHigh
   intKlow = intI + 1
   For intK = intkLow To intkHigh
      If arrArray [intI] %strComparer% arrArray [intK]
         anyTemp = arrArray [intI]
         arrArray [intI] = arrArray [intK]
         arrArray [intK] = anyTemp
      EndIf
   Next
Next
strItemList = arrArray [0]
For intK = 1 To intKHigh
   strItemList = StrCat (strItemList, strDelimiter, arrArray [intK])
Next
Return strItemList
;..........................................................................................................................................
; This UDF "udfItemListSortNumV2" sorts an itemlist of integer and/or float numbers in ascending or descending direction.
; The return value is an itemlist of sorted integer and/or float numbers.
;
; Example:
; This function "udfItemListSortNum" uses the Array Bubble Sort algorithm.
;
; strItemList = "8,1,2,3,4,5,-1.23,-1,16.00,7.9,7.8,9,10,11,12,13,0,3,-0.1,+0.1"
;
; iDirection = @ASCENDING
; strItemList = "-1.23,-1,-0.1,0,+0.1,1,2,3,3,4,5,7.8,7.9,8,9,10,11,12,13,16.00"
;
; iDirection = @DESCENDING
; strItemList = "16.00,13,12,11,10,9,8,7.9,7.8,5,4,3,3,2,1,+0.1,0,-0.1,-1,-1.23"
;
; Note: The array stores items "as is" (numbers, strings, ...) but the items will be compared as numbers.
;
; Detlev Dalitz.20030102.20090430.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListSortFlt (strItemList, strDelimiter, intDirection)
If strItemList == "" Then Return ""
intCount = ItemCount (strItemList, strDelimiter)
hdlBB = BinaryAlloc (intCount * 8)
intHigh = intCount - 1
For intElem = 0 To intHigh
   BinaryPokeFlt (hdlBB, intElem * 8, ItemExtract (intElem + 1, strItemList, strDelimiter))
Next
BinarySort (hdlBB, 8, 0, 8, @FLOAT8|intDirection)
strItemList = ""
For intElem = 0 To intHigh
   strItem = BinaryPeekFlt (hdlBB, intElem * 8)
   intItem = Int (strItem)
   If intItem == strItem Then strItemList = ItemInsert (intItem, -1, strItemList, strDelimiter)
      Else strItemList = ItemInsert (strItem, -1, strItemList, strDelimiter)
Next
hdlBB = BinaryFree (hdlBB)
Return strItemList
;..........................................................................................................................................
; This UDF "udfItemListSortFlt" sorts an itemlist of integer and/or float numbers in ascending or descending direction.
; The return value is an itemlist of sorted integer and/or float numbers.
;
; Example:
; strItemList = "8,1,5.5566,-1,300,-34,.1,16,0"
;
; iDirection = @ASCENDING
; strItemList = "-34,-1,0,0.1,1,5.5566,8,16,300"
;
; iDirection = @DESCENDING
; strItemList = "300,16,8,5.5566,1,0.1,0,-1,-34"
;
; Detlev Dalitz.20020915.20090430.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListSortByteComp (strItemList, strDelimiter)
If strItemList == "" Then Return ""
intCount = ItemCount (strItemList, strDelimiter)
If StrLen (strItemList) == intCount - 1 Then Return strItemList
intBBSizeMax = 0
hdlBB = 0
For intItem = 1 To intCount
   strItem = ItemExtract (intItem, strItemList, strDelimiter)
   If strItem != ""
      intBBSize = StrLen (strItem)
      If intBBSize > intBBSizeMax
         intBBSizeMax = intBBSize
         If hdlBB Then hdlBB = BinaryFree (hdlBB)
      EndIf
      If !hdlBB Then hdlBB = BinaryAlloc (intBBSizeMax)
      strItemList = ItemReplace (BinaryPeekHex (hdlBB, 0, BinaryPokeStr (hdlBB, 0, strItem)), intItem, strItemList, strDelimiter)
   EndIf
Next
strItemList = ItemSort (strItemList, strDelimiter)
For intItem = 1 To intCount
   strItem = ItemExtract (intItem, strItemList, strDelimiter)
   If strItem != "" Then strItemList = ItemReplace (BinaryPeekStr (hdlBB, 0, BinaryPokeHex (hdlBB, 0, strItem)), intItem, strItemList, strDelimiter)
Next
If hdlBB Then hdlBB = BinaryFree (hdlBB)
Return strItemList
;..........................................................................................................................................
; This UDF "ItemSortByteComp" sorts a list of alphanumerical values in ascending direction
; and respects the character byte sort order.
;
; Detlev Dalitz.20090510.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListSortByteComp_ (strItemList, strDelimiter) ; The 'step by step' version.
If strItemList == "" Then Return ""
intCount = ItemCount (strItemList, strDelimiter)
If StrLen (strItemList) == intCount - 1 Then Return strItemList
intBBSizeMax = 0
hdlBB = 0
For intItem = 1 To intCount
   strItem = ItemExtract (intItem, strItemList, strDelimiter)
   If strItem != ""
      intBBSize = StrLen (strItem)
      If intBBSize > intBBSizeMax
         intBBSizeMax = intBBSize
         If hdlBB Then hdlBB = BinaryFree (hdlBB)
      EndIf
      If !hdlBB Then hdlBB = BinaryAlloc (intBBSizeMax)
      intResult = BinaryPokeStr (hdlBB, 0, strItem)
      strHex = BinaryPeekHex (hdlBB, 0, intResult)
      strItemList = ItemReplace (strHex, intItem, strItemList, strDelimiter)
   EndIf
Next
strItemList = ItemSort (strItemList, strDelimiter)
For intItem = 1 To intCount
   strItem = ItemExtract (intItem, strItemList, strDelimiter)
   If strItem != ""
      intResult = BinaryPokeHex (hdlBB, 0, strItem)
      strItem = BinaryPeekStr (hdlBB, 0, intResult)
      strItemList = ItemReplace (strItem, intItem, strItemList, strDelimiter)
   EndIf
Next
If hdlBB Then hdlBB = BinaryFree (hdlBB)
Return strItemList
;..........................................................................................................................................
; This UDF "ItemSortByteComp" sorts a list of alphanumerical values in ascending direction
; and respects the character byte sort order.
;
; Detlev Dalitz.20090510.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;Test.

:Test1
strAlphaNumList = "9A,AAp,Bu,BU,ba7,Apm,Ba2s,Zi,Or,1A"
strResultList11 = ItemSort (strAlphaNumList, ",")             ; "1A,9A,AAp,Apm,Ba2s,ba7,Bu,BU,Or,Zi" ; Native WinBatch ascending sort function.
strResultList12 = udfItemListSortDesc (strAlphaNumList, ",")  ; "Zi,Or,BU,Bu,ba7,Ba2s,Apm,AAp,9A,1A"
strResultList13 = udfItemListSortDescV2 (strAlphaNumList, ",") ; "Zi,Or,BU,Bu,ba7,Ba2s,Apm,AAp,9A,1A"

:Test2
strNumList      = "0,1,-3,5,-7,9,-11,13,15,2,-4,6,-8,10,-12,14"
strResultList21 = udfItemListSortNum (strNumList, ",", @ASCENDING)   ; "-12,-11,-8,-7,-4,-3,0,1,2,5,6,9,10,13,14,15"
strResultList22 = udfItemListSortNum (strNumList, ",", @DESCENDING)  ; "15,14,13,10,9,6,5,2,1,0,-3,-4,-7,-8,-11,-12"

:Test3
strNumList      = "0 1 -3 5 -7 9 -11 13 15 2 -4 6 -8 10 -12 14"
strResultList31 = udfItemListSortNum (strNumList, " ", @ASCENDING)   ; "-12 -11 -8 -7 -4 -3 0 1 2 5 6 9 10 13 14 15"
strResultList32 = udfItemListSortNum (strNumList, " ", @DESCENDING)  ; "15 14 13 10 9 6 5 2 1 0 -3 -4 -7 -8 -11 -12"

:Test4
strFltList      = "8,1,2,3,4,5,-1.23,-1,16.00,7.9,7.8,9,10,11,12,13,0,3,-0.1,+0.1"
strResultList41 = udfItemListSortNumV2 (strFltList, ",", @ASCENDING)  ; "-1.23,-1,-0.1,0,+0.1,1,2,3,3,4,5,7.8,7.9,8,9,10,11,12,13,16.00"
strResultList42 = udfItemListSortNumV2 (strFltList, ",", @DESCENDING) ; "16.00,13,12,11,10,9,8,7.9,7.8,5,4,3,3,2,1,+0.1,0,-0.1,-1,-1.23"

:Test5
strFltList      = "8,1,2,3,4,5,-1.23,-1,16.00,7.9,7.8,9,10,11,12,13,0,3,-0.1,+0.1"
strResultList51 = udfItemListSortFlt (strFltList, ",", @ASCENDING)   ; "-1.23,-1,-0.1,0,0.1,1,2,3,3,4,5,7.8,7.9,8,9,10,11,12,13,16"
strResultList52 = udfItemListSortFlt (strFltList, ",", @DESCENDING)  ; "16,13,12,11,10,9,8,7.9,7.8,5,4,3,3,2,1,0.1,0,-0.1,-1,-1.23"

:Test6
strAlphaNumList = "9A,AAp,Bu,BU,ba7,Apm,Ba2s,Zi,Or,1A"
strResultList61 = udfItemListSortByteComp (strAlphaNumList, ",") ; "1A,9A,AAp,Apm,BU,Ba2s,Bu,Or,Zi,ba7"

strAlphaNumList = ","
strResultList62 = udfItemListSortByteComp (strAlphaNumList, ",") ; ","

strAlphaNumList = ",,"
strResultList63 = udfItemListSortByteComp (strAlphaNumList, ",") ; ",,"

strAlphaNumList = ",,,Bu,BU,ba7,,Ba2s,,"
strResultList64 = udfItemListSortByteComp (strAlphaNumList, ",") ; ",,,,,,BU,Ba2s,Bu,ba7"

Exit