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.

Best way to sort numbers within a tabbed list

Keywords:   sort numbers tabbed lists

Question:

I am trying to sort some numbers that are contained within a tab-delimited list.

Unfortunately when I use ITEMSORT it sorts by string, not by value.

That gives me a number 10 sorted ahead of a number 8. Is there a "number" sort command? What is the best way to accomplish what I need?

Answer:

You can try one ...
;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemsortnum",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemsortnum

#DefineFunction udfItemSortNum (sItemList, sDelimiter, iDirection)
iCount = ItemCount(sItemList,sDelimiter)
sTempList = ""
For i=1 To iCount
   sItem = ItemExtract(i,sItemList,sDelimiter)
   sItem = StrFixLeft(sItem,"",16)
   sTempList = ItemInsert(sItem,-1,sTempList,sDelimiter)
Next
sTempList = ItemSort(sTempList,sDelimiter)
sSortList = ""
iIndexPos = -1 * (iDirection == @ASCENDING)
iIndexNeg = -1 * (iDirection == @DESCENDING)
For i=1 To iCount
   sItem = ItemExtract(i,sTempList,sDelimiter)
   sItem = 0+sItem
   If (sItem >= 0) Then sSortList = ItemInsert(sItem,iIndexPos,sSortList,sDelimiter)
      Else sSortList = ItemInsert(sItem,iIndexNeg,sSortList,sDelimiter)
Next
Return (sSortList)
;..........................................................................................................................................
; sItemList = "0,1,-3,5,-7,9,-11,13,15,2,-4,6,-8,10,-12,14"
;
; iDirection = @ASCENDING
; sSortList = "-12,-11,-8,-7,-4,-3,0,1,2,5,6,9,10,13,14,15"
;
; iDirection = @DESCENDING
; sSortList = "15,14,13,10,9,6,5,2,1,0,-3,-4,-7,-8,-11,-12"
;
; Note: Blank char is forbidden to use as sDelimiter.
;
; Detlev Dalitz.20020915
;..........................................................................................................................................
#EndFunction

:skip_udfitemsortnum
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemsortnumarr",IntControl(77,103,0,0,0),@TAB) Then Goto skip_udfitemsortnumarr

#DefineFunction udfItemSortNumArr (sItemList, sDelimiter, iDirection)
If (sItemList == "") Then Return ("")
sComp = ">"
If (iDirection == @DESCENDING) Then sComp = "<"
aArray = Arrayize(sItemList,sDelimiter)
ikHigh = Max(0,ArrInfo(aArray,1)-1)
iHigh = ikHigh-1
iLow = 0
For i=iLow To iHigh
   ikLow = i+1
   For k=ikLow To ikHigh
      If (aArray[i] %sComp% aArray[k])
         aA = aArray[i]
         aArray[i] = aArray[k]
         aArray[k] = aA
      EndIf
   Next
Next
sItemList = aArray[0]
For ik=1 To ikHigh
   sItemList = StrCat(sItemList,sDelimiter,aArray[ik])
Next

Return (sItemList)
;..........................................................................................................................................
; This function "udfItemSortNumArr" uses the Array Bubble Sort algorithm.
;
; sItemList = "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
; sItemList = "-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
; sItemList = "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: Array stores items "as is" but compares items as numbers.
;
; Detlev Dalitz.20030102
;..........................................................................................................................................
#EndFunction

:skip_udfitemsortnumarr
;------------------------------------------------------------------------------------------------------------------------------------------

;------------------------------------------------------------------------------------------------------------------------------------------
If ItemLocate("udfitemsortflt",IntControl(73,103,0,0,0),@TAB) Then Goto skip_udfitemsortflt

#DefineFunction udfItemSortFlt (sItemList, sDelimiter, iDirection)
iCount = ItemCount(sItemList,sDelimiter)
hBB = BinaryAlloc(iCount*8)
iHigh = iCount - 1
For i=0 To iHigh
   BinaryPokeFlt(hBB,i*8,ItemExtract(i+1,sItemList,sDelimiter))
Next
BinarySort(hBB,8,0,8,@FLOAT8|iDirection)
sItemList = ""
For i=0 To iHigh
   sItem = BinaryPeekFlt(hBB,i*8)
   sInt = Int(sItem)
   If (sInt == sItem) Then sItemList = ItemInsert(sInt,-1,sItemList,sDelimiter)
      Else sItemList = ItemInsert(sItem,-1,sItemList,sDelimiter)
Next
BinaryFree(hBB)
Return (sItemList)
;..........................................................................................................................................
; sItemList = "8,1,5.5566,-1,300,-34,.1,16,0"
;
; iDirection = @ASCENDING
; sItemList = "-34,-1,0,0.1,1,5.5566,8,16,300"
;
; iDirection = @DESCENDING
; sItemList = "300,16,8,5.5566,1,0.1,0,-1,-34"
;
; Detlev Dalitz.20020915
;..........................................................................................................................................
#EndFunction

:skip_udfitemsortflt
;------------------------------------------------------------------------------------------------------------------------------------------


Article ID:   W15757
File Created: 2003:05:13:11:29:56
Last Updated: 2003:05:13:11:29:56