Author Topic: Sorting a list of dates  (Read 3199 times)

mcvpjd3

  • Newbie
  • *
  • Posts: 42
Sorting a list of dates
« 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

dblaze1

  • Newbie
  • *
  • Posts: 11
Re: Sorting a list of dates
« Reply #1 on: November 13, 2014, 08:51:47 am »
Could you convert the dates to a standard format like yyyymmdd then sort?

td

  • Tech Support
  • *****
  • Posts: 4181
    • WinBatch
Re: Sorting a list of dates
« Reply #2 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
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

  • Tech Support
  • *****
  • Posts: 4181
    • WinBatch
Re: Sorting a list of dates
« Reply #3 on: November 13, 2014, 10:23:48 am »
Crude example

Code: Winbatch
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

  • Newbie
  • *
  • Posts: 3
Re: Sorting a list of dates
« Reply #4 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