How Do I Sort an Array of FileInfo by file creation date?

Hi all

I’m trying to sort an array of fileinfo by the creation date (newest files first), is there an easy way to achieve this. The only way I’ve found results in a sorted string array of filepaths which loses all the fileinfo metadata

@AndyD
Have a look here:

new DirectoryInfo(“C:”).GetFiles()
new DirectoryInfo("C:").GetFiles().OrderBy(Function (x) x.CreationTime).ToArray() - return an array of FileInfo

2 Likes

That seems to have worked a treat. It sorts from oldest to newest, how do I swap it around to go newest first?

Actually, just swapped to orderbydescending and it works perfectly. Thanks for your help @ppr

new DirectoryInfo(“C:”).GetFiles().OrderBy(Function (x) x.CreationTime).Reverse().ToArray()
or better
new DirectoryInfo(“C:”).GetFiles().OrderByDescending(Function (x) x.CreationTime).ToArray

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.