views:

21

answers:

2

Am trying to find the latest sub-directory from a parent director.

public static DirectoryInfo GetLatestSubDirectory(string parentDirPath)

As of now the implementation is uses the bubble sort algorithm to find the latest by comparing the creation time.

 if (subDirInfo.CreationTimeUtc > latestSubDirInfo.CreationTimeUtc)

Am wondering if there's a more efficient way to do this? LINQ??

+1  A: 
return new DirectoryInfo(parentDirPath)
           .GetDirectories()
           .OrderByDescending(d => d.CreationTimeUtc)
           .First()
Diego Mijelshon
Thats slick!! Thanks!
Codex
A: 

By latest I guess you mean the newest. I'm wondering why for selecting the minimum / maximum item from a collection people tend to use sorting or LINQ.

DirectoryInfo newest = null;
foreach(string subdirName in Directory.GetDirectory(path))
{
    DirectoryInfo subdirInfo = new DirectoryInfo(subdirName);
    if (newest == null || subdirInfo.CreationTimeUct > newest.CreationTimeUct) 
        newest = subdirInfo;
}
Ondrej Tucny