0

这段代码给了我一个文件夹列表。没有排序它首先出现最旧的。虽然我认为这不能保证。(它可以根据文件名进行排序,即日期)。我想使用 OrderBy 或 OrderByDescending 函数根据创建日期首先对其进行排序。

Dim di As New DirectoryInfo(root)
folderList = di.GetDirectories()

'does not work
folderList.OrderByDescending(Function(x) x.CreationTime)

谢谢

4

1 回答 1

3

您需要(重新)分配OrderByDescending返回给变量的值,它不会对原始集合进行排序。

例如:

folderList = folderList.
    OrderByDescending(Function(x) x.CreationTime).
    ToArray()

另一种选择是对原始数组进行排序:

Array.Sort(folderList, Function(d1, d2) d1.CreationTime.CompareTo(d2.CreationTime))

我正在使用带有Array.SortComparison(Of T).

于 2013-10-18T14:50:50.033 回答