fbpx
Welcome, Guest
Username: Password: Remember me
Visual Objects

Please use this forum to post questions about Visual Objects and Vulcan.NET
  • Page:
  • 1

TOPIC:

VO: Sort Directory by date and time 22 Feb 2021 23:37 #17545

  • Jamal's Avatar

  • Jamal

  • Topic Author


  • Posts: 236
  • Hi,

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

    Thanks!
    Jamal

    Please Log in or Create an account to join the conversation.

    VO: Sort Directory by date and time 23 Feb 2021 05:20 #17546

  • wriedmann's Avatar

  • wriedmann


  • Posts: 2520
  • Hi Jamal,
    that should be easy:
    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

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Last edit: by wriedmann.

    VO: Sort Directory by date and time 23 Feb 2021 05:49 #17547

  • Jamal's Avatar

  • Jamal

  • Topic Author


  • Posts: 236
  • Hi Wolfgang,

    Thank you! With some minor corrections of missing brackets and the dot after .OR., it works great.
    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] ) } )

    Please Log in or Create an account to join the conversation.

    VO: Sort Directory by date and time 23 Feb 2021 05:54 #17548

  • wriedmann's Avatar

  • wriedmann


  • Posts: 2520
  • 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

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    VO: Sort Directory by date and time 23 Feb 2021 07:15 #17549

  • Jamal's Avatar

  • Jamal

  • Topic Author


  • Posts: 236
  • Hi Wolfgang,

    In X#, I created the following sample function which is based on a similar c# code.
    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)

    Please Log in or Create an account to join the conversation.

    Last edit: by Jamal.

    VO: Sort Directory by date and time 25 Feb 2021 11:41 #17566

  • VR's Avatar

  • VR


  • Posts: 12
  • Linq is your friend ;-)

    var files := DirectoryInfo{"C:\"}:GetFiles("*.*"):OrderBy({q => q:CreationTime}):ToList()

    Please Log in or Create an account to join the conversation.

    VO: Sort Directory by date and time 27 Feb 2021 20:12 #17588

  • Jamal's Avatar

  • Jamal

  • Topic Author


  • Posts: 236
  • Hi VR,

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

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1