WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: mcvpjd3 on November 13, 2014, 08:05:10 AM

Title: Sorting a list of dates
Post by: mcvpjd3 on November 13, 2014, 08:05:10 AM
Is there an equivalent of itemsort that works on Dates?

I have a list of dates that I've read in from a file (list consists of Startdate\enddate\numbers\text) and I'd like to sort the list by startdate (format is dd/mm/yyyy). Is there a command to do this or something I can do with itemsort or even a UDF tucked away somewhere that I can use?

Thanks
Title: Re: Sorting a list of dates
Post by: dblaze1 on November 13, 2014, 08:51:47 AM
Could you convert the dates to a standard format like yyyymmdd then sort?
Title: Re: Sorting a list of dates
Post by: td on November 13, 2014, 10:07:00 AM
Normalizing the dates to a format suitable for sorting is a worthy suggestion. One of many ways to do this in WinBatch is the following

Code (winbatch) Select

Date = ObjectType('date', '11/13/2014')
Title: Re: Sorting a list of dates
Post by: td on November 13, 2014, 10:23:48 AM
Crude example

Code (winbatch) Select

lDates = '11/13/2014':@Tab:'09/01/2014':@Tab:'01/13/2013'
nDates = ItemCount(lDates, @tab)
lNorm = ''
for i = 1 to nDates
   Day = ItemExtract(i, lDates, @Tab)
   lNorm = ItemInsert(ObjectType('date', Day), i, lNorm, @LF)
next
lNorm = ItemSort(lNorm, @lf)

Message("Dates Sorted", lNorm)
exit


There are many other ways to sort by date using WinBatch.  The best approach depends on your goals, the format of the data in the file and the external tools you have at your disposal.
Title: Re: Sorting a list of dates
Post by: mattcarl on August 25, 2022, 07:16:49 AM
Sorting a list of POSITIVE numbers (pipe delimited in this example)

unsortedString =  "3|11|9|122|10"
cnt = ItemCount( unsortedString, "|")
paddedString = ""

For s= 1 to cnt
     nextValue = ItemExtract(s, unsortedString, "|")
     paddedString  =  paddedString :  StrFixLeft(nextValue,"0",6) : "|"
Next
;;;;;   padded string will be  "000003|000011|000009| etc....."

sortedString  =  ItemSort( paddedString, "|")
newString = StrReplace( sortedString,           "|0000", "|" )
newString = StrReplace( newString,           "|000", "|" )
newString = StrReplace( newString,           "|00", "|" )
newString = StrReplace( newString,           "|0", "|" )

;;;;; result    3|9|10|11|122