我有一个链接到 Datapager 的两列 ListView。ListView 列出了特定目录中特定类型的所有文件,因此数据源是 FileInfo 类型的 ArrayList。因此,我必须创建一个 Comparer 类以在 ArrayList.Sort 方法中使用。
文件信息比较器.vb:
Imports System.IO
Friend Class FileInfoDateComparer
Implements IComparer
Public Overridable Overloads Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim oX As FileInfo = CType(x, FileInfo)
Dim oY As FileInfo = CType(y, FileInfo)
Return oX.LastWriteTime.CompareTo(oY.LastWriteTime)
End Function
End Class
Friend Class FileInfoNameComparer
Implements IComparer
Public Overridable Overloads Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
Dim oX As FileInfo = CType(x, FileInfo)
Dim oY As FileInfo = CType(y, FileInfo)
Return oX.Name.CompareTo(oY.Name)
End Function
End Class
GenerateSortedArray 子:
Private Sub GenerateSortedArray(ByVal SortColumn As String, ByVal SortDirection As String)
Dim dirInfo As New DirectoryInfo(Server.MapPath(AppSettings.Item("ContentDir")))
Dim FileArrayList As New ArrayList(dirInfo.GetFiles("*.msg", SearchOption.TopDirectoryOnly))
With FileArrayList
.TrimToSize()
Select Case SortColumn
Case "Name"
.Sort(New FileInfoNameComparer)
Case Else
.Sort(New FileInfoDateComparer)
End Select
End With
ListView_排序:
Session("SortColumn") = e.SortExpression
Session("SortDirection") = e.SortDirection
Call GenerateSortedArray(e.SortExpression, DBNull.Value.ToString)
所有这一切都很好,但是,我不知道如何反转排序。从代码中可以看出,我已经确定了排序方向,但我不知道如何使用它来反转排序。
有什么想法吗?