Hi,
Does anyone have a function to sort the files returned by the Directory() function by date and time?
Thanks!
Jamal
VO: Sort Directory by date and time
VO: Sort Directory by date and time
Hi Jamal,
that should be easy:
Wolfgang
that should be easy:
Code: Select all
aDir := Directory( "*.*" )
ASort( aDir,,,{|a1,a2| a1[F_DATE] < a2[F_DATE] .or ( a1[F_DATE == a2[F_DATE .and. a1[F_TIME] <= a2[F_TIME] ) } )
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
VO: Sort Directory by date and time
Hi Wolfgang,
Thank you! With some minor corrections of missing brackets and the dot after .OR., it works great.
Thank you! With some minor corrections of missing brackets and the dot after .OR., it works great.
Code: Select all
aDir := Directory( "*.*" )
ASort( aDir,,, {|a1,a2| a1[F_DATE] < a2[F_DATE] .or. ( a1[F_DATE] == a2[F_DATE] .and. a1[F_TIME] <= a2[F_TIME] ) } )
VO: Sort Directory by date and time
Hi Jamal,
ok, the bracket had I seen myself after posting, but the missing dot not....
I like the ASort() function very much! And I'm needing it very, very often!
Unfortunately the sorting routines in .NET are a bit harder to code, but they work in a similar manner.
Wolfgang
ok, the bracket had I seen myself after posting, but the missing dot not....
I like the ASort() function very much! And I'm needing it very, very often!
Unfortunately the sorting routines in .NET are a bit harder to code, but they work in a similar manner.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
VO: Sort Directory by date and time
Hi Wolfgang,
In X#, I created the following sample function which is based on a similar c# code.
Hope it helps someone B)
In X#, I created the following sample function which is based on a similar c# code.
Code: Select all
USING System
USING System.Collections.Generic
USING System.Linq
USING System.Text
using System.IO
FUNCTION Start() AS VOID
local path as string
local filesArray as FileInfo[]
path := "C:somefolder"
filesArray := DirectoryInfo{path}:GetFiles("*.*")
Array.Sort(filesArray, {x as FileInfo , y as FileInfo => Comparer<DateTime>.Default:Compare(x:CreationTime, y:CreationTime)})
// the above can be also written as:
// Array.Sort(filesArray, {x, y => Comparer<DateTime>.Default:Compare(x:CreationTime, y:CreationTime)})
foreach fi as FileInfo in filesArray
Console.WriteLine(fi:FullName + " " + fi:CreationTime:ToString())
next
Console.ReadKey()
return
VO: Sort Directory by date and time
Linq is your friend
Code: Select all
var files := DirectoryInfo{"C:"}:GetFiles("*.*"):OrderBy({q => q:CreationTime}):ToList()
VO: Sort Directory by date and time
Hi VR,
One liners are great and Linq is very powerful! Thanks for the contribution.
One liners are great and Linq is very powerful! Thanks for the contribution.