Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need some help with a ListView comparer Class

Posted on 2014-02-27
2
Medium Priority
?
246 Views
Last Modified: 2014-02-28
Experts, I have this Listview Comparer Class that i have found and it works really well for most of my columns. However it does not seem to work correctly on columns where the data is formatted as currency. Can you please show me how to modify this class to sort currency columns properly?

Thanks...

'*******************************************************************************************************
'* This class was found on http://http://www.vb-helper.com/howto_net_listview_sort_clicked_column.html *
'* Thanks to Kevin G for the improved caomparison code that tests numeric values as numbers rather     *
'* strings. (For example, as strings "9" comes after "100". But as numbers "9" comes before "100".)    *
'* Thanks to Joe Mestrovich for adding support for date columns. (For example, as strings "10/10/2000" *
'* comes before "1/1/1999". But as dates "10/10/2000" comes after "1/1/1999". Also as strings          *
'* "01/20/2009" comes before "1/1/2009". But as dates "01/20/2009" comes after "1/1/2009".)            *
'* No modifications have been made to this class.                                                      *
'*******************************************************************************************************

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

        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:Basicfarmer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 39894433
I'd start by changing this section:
        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

Open in new window

To:
        If m_SortOrder = SortOrder.Ascending Then

            Dim dec_x As Decimal, dec_y As Decimal
            Dim flags As System.Globalization.NumberStyles = Globalization.NumberStyles.AllowCurrencySymbol Or System.Globalization.NumberStyles.AllowDecimalPoint

            If Decimal.TryParse(string_x, flags, Nothing, dec_x) And Decimal.TryParse(string_y, flags, Nothing, dec_y) Then

                Return dec_x.CompareTo(dec_y)

            ElseIf IsDate(string_x) And IsDate(string_y) Then

Open in new window

And then do the same for the "descending" section too.
0
 

Author Closing Comment

by:Basicfarmer
ID: 39895399
Beautiful, Thank you...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This video teaches viewers about errors in exception handling.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

610 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