Solved

vb.net listview sort items

Posted on 2013-11-01
7
638 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

19 Experts available now in Live!

Get 1:1 Help Now