FileInfoComparer 排序不正确(在 LastWriteTime 上)
我有一个简单的(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
I have a simple (ASP.NET) web page that lists files of a particular type in a folder and renders them in a ListView.
I've tried to sort them by descending LastWriteTime, that is, in reverse date order. However, although the sort process does change the order of the array, it doesn't sort it correctly. For example, one item with a LastWriteTime of #6/3/2011 12:00:00
is top of the list, but another item with a LastWriteTime of #12/16/2011 12:00:00
is halfway down the list after sorting.
Any ideas why?
Code:
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 class:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
LastWriteTime 可以作为字符串返回,这解释了排序顺序。值按字符串排序,而不是 DateTime 对象。将字符串解析为日期时间并且排序顺序应该正确。
LastWriteTime may be returned as a string, which explains the sort order. the values are sorted as strings, not DateTime objects. parse the string to datetime and the sort order should be correct.
这是使用 LINQ 的解决方案。这是用 C# 编写的,并转换为 VB,所以希望这能起作用。
fileList
的类型将为IEnumerable
。如果您需要它是一个可变列表,而不是使用ArrayList
,我建议使用List
,这可以通过添加来完成。 ToList()
然后结束,就像这样Here is a solution that uses LINQ. This was written in C# and converted to VB, so hopefully this works.
The type of
fileList
will beIEnumerable<FileInfo>
. If you need it to be a a mutable list, rather than using anArrayList
, I would recommend using aList<FileInfo>
, which can be done by adding.ToList()
to then end, like so