Solved

How do I improve my listview sort utility to sort numerically.

Posted on 2006-11-30
6
223 Views
Last Modified: 2010-04-23
Hello

I have some simple code to sort a listview (3 columns wide). When a user clicks the column header it will sort ascending or decending depending on the current state. Unfortunately it sorts numbers ascending as if they were text:

1
11
111
2
22
222
etc

I want it to sort ascending like:

1
2
11
22
111
222

Also the reverse (descending is true too)

Any help is appreciated

    Dim pooX As Boolean
    Private Sub lstMenu_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles lstMenu.ColumnClick
        Dim lstCol As ColumnHeader
        If pooX Then
            lstMenu.Sorting = SortOrder.Ascending
            pooX = False
        Else
            lstMenu.Sorting = SortOrder.Descending
            pooX = True
        End If
    End Sub
0
Comment
Question by:PBLack
[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
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18048141
0
 

Author Comment

by:PBLack
ID: 18048214
That seems like a lot of complicated code for something that is so simple. I mean, to sort as if it were text is only 11 lines of code. It just blows me away that I have to make something that complicated to get such a simple result.

I will wait on this for a bit to see what others have to say. I appreciate the help thus far.

PBLack
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18048228
sorry, but MS did never try to sort a listview based on numerical data (as it seems).
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 15

Expert Comment

by:Solar_Flare
ID: 18049255
that example is quite a lot of code - you could get away with far less, but you still need to effectively do the sorting (or at least the comparison operation for the sorting) with your own code.


in .NET 2.0 the datagridview can be used in a similar way to a details-view listview and its sorting works properly for numerics and dates. (listview sorts dates alphabetically too)
0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 18050261
A workaround for this is to left-pad your numbers with spaces so that they are all the same length.  So if 222 (or 999) is the highest number in the listview column, rather than filling with "1" you fill with "  1".  Code to fill would be on the lines of

   myListViewItem = myValue.ToString.PadLeft(myLength)

And for reading back it would be on the lines of

   myValue = CInt(myListViewItem)

Roger
0
 

Author Comment

by:PBLack
ID: 18053500
Sancler for my purposes this was exactly what I needed.

Thanks for this and thanks to the others for their help

PBLack
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…

738 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