I have a two-column ListView linked to a Datapager. The ListView lists all files of a particular type in a particular directory, so the data source is an ArrayList of type FileInfo. Consequently, I had to create a Comparer class to use in the ArrayList.Sort method.
FileInfoComparer.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 sub:
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_Sorting:
Session("SortColumn") = e.SortExpression
Session("SortDirection") = e.SortDirection
Call GenerateSortedArray(e.SortExpression, DBNull.Value.ToString)
All this works well, however, I don't know how to reverse the sort. As you can see from the code, I've determined the Sort Direction, but I don't know how to use it to reverse the sort.
Any thoughts?