Sort directory by last modified

Viewed 91

I'd like to sort a directory by the files that were last modified in raku. I'm getting the list of files like this:

"directory/".IO.dir;

Is there a simple way to do this?

1 Answers

.dir returns a sequence of IO::Path objects.

The IO::Path object has a .modified method that returns the Instant the directory entry was modified.

Combine the all of this:

say "$_: $_.modified.DateTime()" for "lib".IO.dir.sort(*.modified);

And you get a list of files in the "lib" directory, with their modification DateTime in ascending order.

To get it in descending order, either use -*.modified, or call .reverse on the result.

As to sort performance: when you call sort with a Callable that accepts a single argument, it will do a Schwartzian transform for you under the hood.

Related