Hello,

I need to add the functionality to my listview contols so that they can be sorted by clicking on the column headers. In one of my list views I may have a lot of items - like a couple of thousand maybe.

The problem I have is in my sorter class in the compare method. I want my LV to sort by the data type that it is, ie if it's a date I don't want to sort it like a string! I have the following code and it is very very slow when sorting strings, but reasonable (just!) when doing dates which I presume is due to all the parsing I am doing

Can you give me an idea on how to better write this code?

Also, is there a better sorting method than this? Obv, a bubble sort (which I presume it is) isn't going to be best when dealing with a lot of data.

Code:
    Public Class ListViewSorter
        Implements System.Collections.IComparer

        Private _SortIndex As Integer
        Private _SortOrder As SortOrder

        Public Sub New(ByVal SortIndex As Integer, ByVal sortorder As SortOrder)
            _SortIndex = SortIndex
            _SortOrder = sortorder
        End Sub

        Public Function Compare_(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
            'Cast them to LVI's
            Dim lviX As ListViewItem = CType(x, ListViewItem)
            Dim lviY As ListViewItem = CType(y, ListViewItem)

            'Get the strings out of em that we need to compare
            Dim strX As String = lviX.SubItems(_SortIndex).Text
            Dim strY As String = lviY.SubItems(_SortIndex).Text

            Try ' See if we have a date
                Dim dteX As Date = DateTime.Parse(strX)
                Dim dteY As Date = DateTime.Parse(strY)

                If _SortOrder = SortOrder.Ascending Then
                    Return DateTime.Compare(dteX, dteY)
                Else
                    Return DateTime.Compare(dteY, dteX)
                End If

            Catch
                Try ' See if we have a numeric
                    Dim intX As Integer = Integer.Parse(strX)
                    Dim inty As Integer = Integer.Parse(strY)
                    If intX > inty Then
                        If _SortOrder = SortOrder.Ascending Then
                            Return 1
                        Else
                            Return -1
                        End If
                    ElseIf intX < inty Then
                        If _SortOrder = SortOrder.Ascending Then
                            Return -1
                        Else
                            Return 1
                        End If
                    Else
                        Return 0
                    End If
                Catch 'Just sort as a string
                    If _SortOrder = SortOrder.Ascending Then
                        Return String.Compare(strX, strY)
                    Else
                        Return String.Compare(strY, strX)
                    End If
                End Try
            End Try
        End Function
    End Class