Sorting a list of dates

Started by mcvpjd3, November 13, 2014, 08:05:10 AM

Previous topic - Next topic

mcvpjd3

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

dblaze1

Could you convert the dates to a standard format like yyyymmdd then sort?

td

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')
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

mattcarl

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