Determine the datatype of the columns to sort in a listview


How can you determine the datatype of the column of a listview so you can sort it properly e.g, if its numeric, string or date.

I am doing a sort on a listview and it always treat the column as string, that's why it doesn't sort properly on numeric values.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Public Class ListViewSorter
    Implements IComparer
    Private mNumeric As Boolean, mDate As Boolean, ClickedColumn As Integer, isAscending As Boolean = True

    Public Sub New(ByRef ListView As System.Windows.Forms.ListView, ByVal ivlClickedColumn As Integer, ByVal bvlisAscending As Boolean)
        ClickedColumn = ivlClickedColumn
        isAscending = bvlisAscending
        mNumeric = False
        mDate = False
        If ListView.Items.Count > 0 Then
            If Microsoft.VisualBasic.IsNumeric(ListView.Items(0).SubItems(ClickedColumn).Text.ToUpper.Trim) Then
                mNumeric = True
            ElseIf Microsoft.VisualBasic.IsDate(ListView.Items(0).SubItems(ClickedColumn).Text.ToUpper.Trim) Then
                mDate = True
            End If
        End If
    End Sub

    Private Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
            Dim lv1 As ListViewItem = x
            Dim lv2 As ListViewItem = y
            If mNumeric Then
                If isAscending = True Then
                    Return CDbl(lv1.SubItems(ClickedColumn).Text) + CDbl(lv2.SubItems(ClickedColumn).Text)
                    Return CDbl(lv1.SubItems(ClickedColumn).Text) - CDbl(lv2.SubItems(ClickedColumn).Text)
                End If
            ElseIf mNumeric = False And mDate = False Then
                If isAscending = True Then
                    Return String.Compare(lv2.SubItems(ClickedColumn).Text, lv1.SubItems(ClickedColumn).Text)
                    Return String.Compare(lv1.SubItems(ClickedColumn).Text, lv2.SubItems(ClickedColumn).Text)
                End If
                If isAscending = True Then
                    Return Date.Compare(CDate(Microsoft.VisualBasic.FormatDateTime(lv2.SubItems(ClickedColumn).Text, Microsoft.VisualBasic.DateFormat.LongDate)), CDate(Microsoft.VisualBasic.FormatDateTime(lv1.SubItems(ClickedColumn).Text, Microsoft.VisualBasic.DateFormat.LongDate)))
                    Return Date.Compare(CDate(Microsoft.VisualBasic.FormatDateTime(lv1.SubItems(ClickedColumn).Text, Microsoft.VisualBasic.DateFormat.LongDate)), CDate(Microsoft.VisualBasic.FormatDateTime(lv2.SubItems(ClickedColumn).Text, Microsoft.VisualBasic.DateFormat.LongDate)))
                End If
            End If
        Catch ex As Exception
        End Try
    End Function
End Class

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MsFoxAuthor Commented:
Thanks.  Exactly what I needed.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.