I need to sort the columns of a ListView.
Some columns contain dates and others contain integers.
What I did once before is in the Compare method I tried date and if that
failed I did Integer.
Seems kinda not nice - is there a better way?
Thanks
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compa re
-snip
''Cast the objects to be compared to ListViewItem objects.
lListviewX = CType(x, ListViewItem)
lListviewY = CType(y, ListViewItem)
Try
'Parse the two objects passed as a parameter as a DateTime.
Dim lFirstDate As System.DateTime =
DateTime.Parse( lListviewX.SubI tems(mColumnToS ort).Text)
Dim lSecondDate As System.DateTime =
DateTime.Parse( lListviewY.SubI tems(mColumnToS ort).Text)
'Compare the two dates.
lCompareResult = DateTime.Compar e(lFirstDate, lSecondDate)
' Catch
'If neither compared object has a valid date format compare the two items
as a string
lCompareResult =
mInsensitiveCom pare.Compare(lL istviewX.SubIte ms(mColumnToSor t).Text,
lListviewY.SubI tems(mColumnToS ort).Text) 'Or can do this
End Try
Some columns contain dates and others contain integers.
What I did once before is in the Compare method I tried date and if that
failed I did Integer.
Seems kinda not nice - is there a better way?
Thanks
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
Implements IComparer.Compa re
-snip
''Cast the objects to be compared to ListViewItem objects.
lListviewX = CType(x, ListViewItem)
lListviewY = CType(y, ListViewItem)
Try
'Parse the two objects passed as a parameter as a DateTime.
Dim lFirstDate As System.DateTime =
DateTime.Parse( lListviewX.SubI tems(mColumnToS ort).Text)
Dim lSecondDate As System.DateTime =
DateTime.Parse( lListviewY.SubI tems(mColumnToS ort).Text)
'Compare the two dates.
lCompareResult = DateTime.Compar e(lFirstDate, lSecondDate)
' Catch
'If neither compared object has a valid date format compare the two items
as a string
lCompareResult =
mInsensitiveCom pare.Compare(lL istviewX.SubIte ms(mColumnToSor t).Text,
lListviewY.SubI tems(mColumnToS ort).Text) 'Or can do this
End Try
Comment