Solved

Sorting Numerically in a ListView

Posted on 2006-07-04
4
271 Views
Last Modified: 2010-04-23
I am using the following code to allow me to sort a listview by clicking on the column headers

Public m_SortingColumn As ColumnHeader

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

        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)
            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
            If m_SortOrder = SortOrder.Ascending Then
                Return String.Compare(string_x, string_y)
            Else
                Return String.Compare(string_y, string_x)
            End If
        End Function
    End Class

 Private Sub lstvwColumns_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lstvwColumns.ColumnClick
        Dim colTemp As ColumnHeader = lstvwColumns.Columns(e.Column)
        Dim sort_order As System.Windows.Forms.SortOrder
        If m_SortingColumn Is Nothing Then
            sort_order = SortOrder.Ascending
        Else
            If colTemp.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 = colTemp
        If sort_order = SortOrder.Ascending Then
            m_SortingColumn.Text = "> " & m_SortingColumn.Text
        Else
            m_SortingColumn.Text = "< " & m_SortingColumn.Text
        End If
        lstvwColumns.ListViewItemSorter = New ListViewComparer(e.Column, sort_order)
        lstvwColumns.Sort()
    End Sub

This works great, however, when it sorts a numeric column it sorts one digit at a time rather than whole numbers ie: it puts 1000 before 2 when sorting ascending.
Is there anyway my code can by modified to allow any numeric columns to be correctly numerically sorted without affecting the sort order of normal text columns

Thanks
0
Comment
Question by:kiranboi
  • 2
4 Comments
 
LVL 9

Expert Comment

by:lojk
Comment Utility
load your values into a dataset, ensuring your numeric column is set to be a number rather than text and bind the listview to that rather than your list.
0
 

Author Comment

by:kiranboi
Comment Utility
any chance of a code example. Im a newbie :o)
0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
Comment Utility
As a non-dataset approach, you could try adding .PadLeft(10, " ") to

                string_x = item_x.SubItems(m_ColumnNumber).Text

and

                string_y = item_y.SubItems(m_ColumnNumber).Text

in your Public Function Compare.  That is, change them, respectively, to

                string_x = item_x.SubItems(m_ColumnNumber).Text.PadLeft(10, " ")

and

                string_y = item_y.SubItems(m_ColumnNumber).Text.PadLeft(10, " ")


The effect of this would be to base the string comparison on the full 10 character, space-padded, string that resulted in each case and, for numerals, (using '-' to indicate the spaces just for the purposes of this post) "---------2" would be 'less than' "------1000".

It's a bit rough and ready as it stands.  The 10 is fairly arbitrary, but assumes that none of the numbers that you want to sort will be represented by more than 10 characters.  It will not take account of numerals expressed to different degrees of decimal precision: or of the fact that numbers preceded by a minus sign should be treated as 'less than' those not so preceded.  It could possibly have some impact on some alpha sorts.  But it could be refined to cope with each of those problems if they would, in practice, be problems.

Roger
0
 

Author Comment

by:kiranboi
Comment Utility
That'll do nicely. Thanks Roger
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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

12 Experts available now in Live!

Get 1:1 Help Now