WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

List Manipulation

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

ItemSort UDFs

 Keywords: ItemSort Numbers Float udfItemListSortOrdinal  udfItemListSortDesc udfItemListSortDesc2 udfItemListSortNum udfItemListSortNum2 udfItemListSortFlt

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfItemListSortOrdinal (strItemList, strDelimiter, intDirection)
If intDirection != @ASCENDING && intDirection != @DESCENDING Then intDirection = @ASCENDING
If strItemList == "" Then Return ""
intCount = ItemCount (strItemList, strDelimiter)
If StrLen (strItemList) == intCount - 1 Then Return strItemList

; Get max item length, my sophisticated invention.
intItemLenMax = 0
intBBSize = 1 + intCount + StrLen (strItemList)
hdlBB = BinaryAlloc (intBBSize)
BinaryPokeStr (hdlBB, 1, StrClean (strItemList, strDelimiter, " ", @TRUE, 2))
BinaryReplace (hdlBB, strDelimiter, @CRLF, @TRUE)
BinaryPokeStr (hdlBB, intBBSize - 1, @CR)
BinaryPokeStr (hdlBB, 0, @LF)
strSearch = StrCat (@LF, @CR)
While @TRUE
   BinaryReplace (hdlBB, strSearch, "", @TRUE)
   If BinaryEodGet (hdlBB) <= intItemLenMax Then Break
   intItemLenMax = intItemLenMax + 1
   strSearch = StrCat (@LF, StrFill (" ", intItemLenMax), @CR)
EndWhile
hdlBB = BinaryFree (hdlBB)

; Ordinal sort, using BinarySort string sort with hex strings.
intHigh = intCount - 1
intRecSize = intItemLenMax * 2
hdlBB = BinaryAlloc (intItemLenMax)
hdlBBSort = BinaryAlloc (intCount * intRecSize)
For intI = 0 To intHigh
   strItem = ItemExtract (intI + 1, strItemList, strDelimiter)
   If strItem != "" Then BinaryPokeStr (hdlBBSort, intI * intRecSize, BinaryPeekHex (hdlBB, 0, BinaryPokeStr (hdlBB, 0, strItem)))
Next
BinarySort (hdlBBSort, intRecSize, 0, intRecSize, @STRING | intDirection)
strItemList = ""
For intI = 0 To intHigh
   strItem = BinaryPeekStr (hdlBBSort, intI * intRecSize, intRecSize)
   strItemList = StrCat (strItemList, strDelimiter)
   If strItem != "" Then strItemList = StrCat (strItemList, BinaryPeekStr (hdlBB, 0, BinaryPokeHex (hdlBB, 0, strItem)))
Next
hdlBBSort = BinaryFree (hdlBBSort)
hdlBB = BinaryFree (hdlBB)

Return StrSub (strItemList, 2, -1)
;..........................................................................................................................................
; This UDF "udfItemListSortOrdinal" sorts a list of alphanumerical values in ascending or descending direction
; and respects the ordinal character series.
;
; Ordinal means, that each byte of each string is binary compared "as is".
; Uppercase characters are placed before lowercase characters in standard ASCII order.
;
; Detlev Dalitz.20090513.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------;;;
;------------------------------------------------------------------------------------------------------------------------------------------
#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 udfItemListSortDesc2 (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 "udfItemListSortDesc2" 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 udfItemListSortNum2 (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 "udfItemListSortNum2" 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
;------------------------------------------------------------------------------------------------------------------------------------------


;Test.

:Test1
strAlphaNumList = "9A,AAp,Bu,ba7,Apm,Ba2s,Zi,Or,1A"
strResultList11 = ItemSort (strAlphaNumList, ",")             ; "1A,9A,AAp,Apm,Ba2s,ba7,Bu,Or,Zi" ; Native WinBatch ascending sort function.
strResultList12 = udfItemListSortDesc (strAlphaNumList, ",")  ; "Zi,Or,Bu,ba7,Ba2s,Apm,AAp,9A,1A"
strResultList13 = udfItemListSortDesc2 (strAlphaNumList, ",") ; "Zi,Or,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 = udfItemListSortNum2 (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 = udfItemListSortNum2 (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"

Exit

Article ID:   W18373
Filename:   ItemSort UDFs.txt
File Created: 2009:05:13:09:48:38
Last Updated: 2009:05:13:09:48:38