排序文件按日期文件、按日

2023-09-04 01:51:48 作者:背影狠孤单

我使用这样的code比较文件按日期排序。

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?

在一个时间,我只能比较2项...

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

我无法通过得到的所有文件目录进行排序。

I could not sort by getting all files from directory.

推荐答案

您可以使用LINQ:

var sortedFiles = new DirectoryInfo(@"D:\samples").GetFiles()
                                                  .OrderBy(f => f.LastWriteTime)
                                                  .ToList();