Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting Files by date

Tags:

c#

.net

I am using such code to compare files to sort by date..

FileInfo f = new FileInfo(name1);
FileInfo f1 = new FileInfo(name2);
if (f.Exists && f1.Exists)
   output = DateTime.Compare(f.LastWriteTime, f1.LastWriteTime);

Is there any better and faster way to sort by Date?

At a time i can compare only 2 items...

I could not sort by getting all files from directory.

like image 937
curiosity Avatar asked Feb 19 '11 06:02

curiosity


People also ask

How do I sort files by Date and time?

Click the sort option in the top right of the Files area and select Date from the dropdown. Once you have Date selected, you will see an option to switch between descending and ascending order.

How do you sort files in chronological order?

Simply select the 'Edit Details' option and select 'Show Document Date on Screen'. Arrange Documents Chronologically. Sorting document in date order can be done with a single click. Select the arrow icon to sort either ascending or descending order.


5 Answers

You can use LINQ:

var sortedFiles = new DirectoryInfo(@"D:\samples").GetFiles()
                                                  .OrderBy(f => f.LastWriteTime)
                                                  .ToList();
like image 124
BrokenGlass Avatar answered Sep 25 '22 05:09

BrokenGlass


    DirectoryInfo directoryInfo = new DirectoryInfo(@"D:\Temp");
    var result = directoryInfo.GetFiles("*.*",SearchOption.AllDirectories).OrderBy(t => t.LastWriteTime).ToList();
like image 32
Alex Burtsev Avatar answered Sep 23 '22 05:09

Alex Burtsev


What about using Array.Sort ?

string[] fileNames = Directory.GetFiles("directory ", "*.*");
DateTime[] creationTimes = new DateTime[fileNames.Length];
for (int i = 0; i < fileNames.Length; i++)
    creationTimes[i] = new FileInfo(fileNames[i]).CreationTime;
Array.Sort(creationTimes, fileNames);
like image 30
Vivek Goel Avatar answered Sep 22 '22 05:09

Vivek Goel


This is another way of doing it for the whole directory:dateCompareFileInfo

if (Directory.Exists(DIRECTORY_NAME))
{
     DirectoryInfo di = new DirectoryInfo(DIRECTORY_NAME);
     FileInfo[] logFiles = di.GetFiles("AN-10-log.txt*");

     DateCompareFileInfo dateCompareFileInfo = new DateCompareFileInfo();

     Array.Sort(logFiles, dateCompareFileInfo);
}

And the you need a new DateCompareFileInfo class that implements IComparer:

class DateCompareFileInfo : IComparer<FileInfo>
{
    /// <summary>
    /// Compare the last dates of the File infos
    /// </summary>
    /// <param name="fi1">First FileInfo to check</param>
    /// <param name="fi2">Second FileInfo to check</param>
    /// <returns></returns>
    public int Compare(FileInfo fi1, FileInfo fi2)
    {
        int result;
        if (fi1.LastWriteTime == fi2.LastWriteTime)
        {
            result = 0;
        }
        else if (fi1.LastWriteTime < fi2.LastWriteTime)
        {
            result = 1;
        }
        else
        {
            result = -1;
        }

        return result;
    }
}
like image 25
AidanO Avatar answered Sep 22 '22 05:09

AidanO


IMPORTANT - When ordering by LastWriteTime, it should be noted that if the file has never been modified, this date may return 1601 or 1600. Here is what MSDN says:

If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

If your timezone is PST, like me the date will actually be 12/31/1600. To get around this and write more robust code you might consider something like this:

.OrderByDescending(f => f.LastWriteTime.Year <= 1601 ? f.CreationTime : f.LastWriteTime)
like image 45
ProVega Avatar answered Sep 25 '22 05:09

ProVega