Solved

vb.net listview sort items

Posted on 2013-11-01
7
640 Views
Last Modified: 2013-11-02
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

0
Comment
Question by:XK8ER
  • 3
  • 3
7 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39618381
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
 
LVL 1

Author Comment

by:XK8ER
ID: 39618385
thanks AndyAinscow, how would I do that?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39618397
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:XK8ER
ID: 39618412
Integer.Parse

nothing happens with the code.. meaning nothing gets sorted with numbers
0
 
LVL 1

Author Comment

by:XK8ER
ID: 39618440
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39618461
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
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 39618849
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A short film showing how OnPage and Connectwise integration works.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now