Page 1 of 1
VO: Sort Directory by date and time
Posted: Mon Feb 22, 2021 10:37 pm
by Jamal
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
Posted: Tue Feb 23, 2021 4:20 am
by wriedmann
Hi Jamal,
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
VO: Sort Directory by date and time
Posted: Tue Feb 23, 2021 4:49 am
by Jamal
Hi Wolfgang,
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
Posted: Tue Feb 23, 2021 4:54 am
by wriedmann
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
VO: Sort Directory by date and time
Posted: Tue Feb 23, 2021 6:15 am
by Jamal
Hi Wolfgang,
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
Hope it helps someone B)
VO: Sort Directory by date and time
Posted: Thu Feb 25, 2021 10:41 am
by VR
Linq is your friend
Code: Select all
var files := DirectoryInfo{"C:"}:GetFiles("*.*"):OrderBy({q => q:CreationTime}):ToList()
VO: Sort Directory by date and time
Posted: Sat Feb 27, 2021 7:12 pm
by Jamal
Hi VR,
One liners are great and Linq is very powerful! Thanks for the contribution.