我有一个简单的 (ASP.NET) 网页,它列出了文件夹中特定类型的文件并将它们呈现在 ListView 中。
我尝试按 LastWriteTime 降序对它们进行排序,也就是说,以相反的日期顺序。然而,虽然排序过程确实改变了数组的顺序,但它并没有正确排序。例如,LastWriteTime 为的一项#6/3/2011 12:00:00
位于列表顶部,但 LastWriteTime 为的另一项#12/16/2011 12:00:00
在排序后位于列表的中间。
任何想法为什么?
代码:
Dim dirInfo As New DirectoryInfo(Server.MapPath(AppSettings.Item("ContentDir")))
Dim FileArrayList As New ArrayList(dirInfo.GetFiles("*.msg", SearchOption.TopDirectoryOnly))
Dim SortDirections As New Dictionary(Of String, SqlClient.SortOrder)
With FileArrayList
.TrimToSize()
.Sort(New FileInfoComparer(SqlClient.SortOrder.Descending, "LastWriteTime"))
End With
FileInforComparer 类:
Imports System.IO
Imports System.Reflection
Public Class FileInfoComparer
Implements IComparer
Private _sortOrder As System.Data.SqlClient.SortOrder
Private _sortColumn As String
''' <summary>
''' Constructs new Comparer object, using the supplied SortOrder and SortColumn parameters
''' </summary>
''' <param name="sortOrder">Defines the SortOrder for the comparison</param>
''' <param name="sortColumn">Defines which column is sorted</param>
''' <remarks></remarks>
Public Sub New(ByVal sortOrder As System.Data.SqlClient.SortOrder, ByVal sortColumn As String)
_sortOrder = sortOrder
_sortColumn = sortColumn
End Sub
''' <summary>
''' Defines the Sorting mechanism for FileInfo objects
''' </summary>
''' <param name="x">First FileInfo object to compare</param>
''' <param name="y">Second FileInfo object to compare</param>
''' <returns></returns>
''' <remarks></remarks>
Public Overridable Overloads Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim oX_PI As PropertyInfo = CType(x, FileInfo).GetType.GetProperty(_sortColumn)
Dim oY_PI As PropertyInfo = CType(y, FileInfo).GetType.GetProperty(_sortColumn)
Dim Result As Int16 = oX_PI.GetValue(x, Nothing).CompareTo(oY_PI.GetValue(x, Nothing))
'If DESC then reverse the result
If _sortOrder = SqlClient.SortOrder.Descending Then Result = Result * -1
Return Result
End Function
End Class