D
**Developer**
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.Compare
-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.SubItems(mColumnToSort).Text)
Dim lSecondDate As System.DateTime =
DateTime.Parse(lListviewY.SubItems(mColumnToSort).Text)
'Compare the two dates.
lCompareResult = DateTime.Compare(lFirstDate, lSecondDate)
' Catch
'If neither compared object has a valid date format compare the two items
as a string
lCompareResult =
mInsensitiveCompare.Compare(lListviewX.SubItems(mColumnToSort).Text,
lListviewY.SubItems(mColumnToSort).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.Compare
-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.SubItems(mColumnToSort).Text)
Dim lSecondDate As System.DateTime =
DateTime.Parse(lListviewY.SubItems(mColumnToSort).Text)
'Compare the two dates.
lCompareResult = DateTime.Compare(lFirstDate, lSecondDate)
' Catch
'If neither compared object has a valid date format compare the two items
as a string
lCompareResult =
mInsensitiveCompare.Compare(lListviewX.SubItems(mColumnToSort).Text,
lListviewY.SubItems(mColumnToSort).Text) 'Or can do this
End Try