FileInfoComparer 排序不正确(在 LastWriteTime 上)

发布于 2024-12-22 15:17:23 字数 2474 浏览 1 评论 0原文

我有一个简单的(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热情消退 2024-12-29 15:17:23

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.

手心的温暖 2024-12-29 15:17:23

这是使用 LINQ 的解决方案。这是用 C# 编写的,并转换为 VB,所以希望这能起作用。

Dim dirInfo = New DirectoryInfo(Server.MapPath(AppSettings.Item("ContentDir")))
Dim fileList = dirInfo.GetFiles("*.msg", SearchOption.TopDirectoryOnly).OrderByDescending(Function(f) f.LastWriteTime)

fileList 的类型将为 IEnumerable。如果您需要它是一个可变列表,而不是使用 ArrayList,我建议使用 List,这可以通过添加 来完成。 ToList() 然后结束,就像这样

Dim fileList = dirInfo.GetFiles("*.msg", SearchOption.TopDirectoryOnly).OrderByDescending(Function(f) f.LastWriteTime).ToList()

Here is a solution that uses LINQ. This was written in C# and converted to VB, so hopefully this works.

Dim dirInfo = New DirectoryInfo(Server.MapPath(AppSettings.Item("ContentDir")))
Dim fileList = dirInfo.GetFiles("*.msg", SearchOption.TopDirectoryOnly).OrderByDescending(Function(f) f.LastWriteTime)

The type of fileList will be IEnumerable<FileInfo>. If you need it to be a a mutable list, rather than using an ArrayList, I would recommend using a List<FileInfo>, which can be done by adding .ToList() to then end, like so

Dim fileList = dirInfo.GetFiles("*.msg", SearchOption.TopDirectoryOnly).OrderByDescending(Function(f) f.LastWriteTime).ToList()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文