vb.net listview sort items

Hello,
I am trying to sort some number with this listview

from this
50
500
1,500
900
5,600

into this
5,600
1,500
900
500
50

but its not working maybe cus of the comma, how can I make it work ?

    Private m_SortingColumn As ColumnHeader
    Private Sub lstMain_ColumnClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lstMain.ColumnClick
        Dim new_sorting_column As ColumnHeader = lstMain.Columns(e.Column)

        Dim sort_order As System.Windows.Forms.SortOrder
        If m_SortingColumn Is Nothing Then
            sort_order = SortOrder.Ascending
        Else
            If new_sorting_column.Equals(m_SortingColumn) Then
                If m_SortingColumn.Text.StartsWith("> ") Then
                    sort_order = SortOrder.Descending
                Else
                    sort_order = SortOrder.Ascending
                End If
            Else
                sort_order = SortOrder.Ascending
            End If

            m_SortingColumn.Text = _
                m_SortingColumn.Text.Substring(2)
        End If

        m_SortingColumn = new_sorting_column
        If sort_order = SortOrder.Ascending Then
            m_SortingColumn.Text = "> " & m_SortingColumn.Text
        Else
            m_SortingColumn.Text = "< " & m_SortingColumn.Text
        End If

        lstMain.ListViewItemSorter = New ListViewComparer(e.Column, sort_order)
        lstMain.Sort()
    End Sub

Open in new window


Public Class ListViewComparer
        Implements IComparer

        Private m_ColumnNumber As Integer
        Private m_SortOrder As SortOrder

        Public Sub New(ByVal column_number As Integer, ByVal sort_order As SortOrder)
            m_ColumnNumber = column_number
            m_SortOrder = sort_order
        End Sub

        ' Compare the items in the appropriate column for objects x and y.
        Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
            Compare = Nothing

                Dim item_x As ListViewItem = DirectCast(x, ListViewItem)
                Dim item_y As ListViewItem = DirectCast(y, ListViewItem)

                ' Get the sub-item values.
                Dim string_x As String
                If item_x.SubItems.Count <= m_ColumnNumber Then
                    string_x = ""
                Else
                    string_x = item_x.SubItems(m_ColumnNumber).Text
                End If

                Dim string_y As String
                If item_y.SubItems.Count <= m_ColumnNumber Then
                    string_y = ""
                Else
                    string_y = item_y.SubItems(m_ColumnNumber).Text
                End If

                ' Compare them.
                If m_SortOrder = SortOrder.Ascending Then
                    If IsNumeric(string_x) And IsNumeric(string_y) Then
                        Return Val(string_x).CompareTo(Val(string_y))
                    ElseIf IsDate(string_x) And IsDate(string_y) Then
                        Return DateTime.Parse(string_x).CompareTo(DateTime.Parse(string_y))
                    Else
                        Return String.Compare(string_x, string_y)
                    End If
                Else
                    If IsNumeric(string_x) And IsNumeric(string_y) Then
                        Return Val(string_y).CompareTo(Val(string_x))
                    ElseIf IsDate(string_x) And IsDate(string_y) Then
                        Return DateTime.Parse(string_y).CompareTo(DateTime.Parse(string_x))
                    Else
                        Return String.Compare(string_y, string_x)
                    End If
                End If

        End Function
    End Class

Open in new window

LVL 1
XK8ERAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
IsNumeric and TryParse are dependant on the culture. They will work with the current set of data only if the Control Panel or the Culture of the application have the thousands separator set to a coma, which is not the case for everyone. In some cultures, the coma is used as the decimal separator, so the stuff would end up all wrong because 5,600 would be seen as 5.6.

The Replace does not have that limitation and is thus a better solution.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Yes, you are sorting strings and 5,300 is less than 6 as a string.

In your sort function you need to convert the string value (900 or 5,300 or..) into the numeric value - eg. with a TryParse.  Then compare the numeric values to determine the
sort order.
0
 
XK8ERAuthor Commented:
thanks AndyAinscow, how would I do that?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
AndyAinscowFreelance programmer / ConsultantCommented:
eg.
instead of
                    If IsNumeric(string_x) And IsNumeric(string_y) Then
                        Return Val(string_x).CompareTo(Val(string_y))

use something like (I usually code in c# or C++ so this might need slight modification)

                    If IsNumeric(string_x) And IsNumeric(string_y) Then
                        Return Int.Parse(string_x) < Int.Parse(string_y)
0
 
XK8ERAuthor Commented:
Integer.Parse

nothing happens with the code.. meaning nothing gets sorted with numbers
0
 
XK8ERAuthor Commented:
I think i've found a solution, right before

If m_SortOrder = SortOrder.Ascending Then

I added this and it works..

                string_y = string_y.Replace(",", "")
                string_x = string_x.Replace(",", "")

Open in new window

0
 
AndyAinscowFreelance programmer / ConsultantCommented:
That is fine - that is a VB method to convert a string to a number.    

According to the help files the Val(...) would stop with the first non number in the string so 5,300 would be converted to 5 - hence your problem
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.