VO: Sort Directory by date and time

Public support forum for peer to peer support with related to the Visual Objects and Vulcan.NET products
Post Reply
Jamal
Posts: 320
Joined: Mon Jul 03, 2017 7:02 pm

VO: Sort Directory by date and time

Post by Jamal »

Hi,

Does anyone have a function to sort the files returned by the Directory() function by date and time?

Thanks!
Jamal
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

VO: Sort Directory by date and time

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Jamal
Posts: 320
Joined: Mon Jul 03, 2017 7:02 pm

VO: Sort Directory by date and time

Post 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] ) } )
User avatar
wriedmann
Posts: 3757
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

VO: Sort Directory by date and time

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Jamal
Posts: 320
Joined: Mon Jul 03, 2017 7:02 pm

VO: Sort Directory by date and time

Post 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)
VR
Posts: 102
Joined: Sun Aug 23, 2020 3:07 pm
Location: Italy

VO: Sort Directory by date and time

Post by VR »

Linq is your friend ;-)

Code: Select all

var files := DirectoryInfo{"C:"}:GetFiles("*.*"):OrderBy({q => q:CreationTime}):ToList()
Jamal
Posts: 320
Joined: Mon Jul 03, 2017 7:02 pm

VO: Sort Directory by date and time

Post by Jamal »

Hi VR,

One liners are great and Linq is very powerful! Thanks for the contribution.
Post Reply