Solved

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

Posted on 2006-11-30
6
221 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
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS.net 2010 11 48
Import a txt file into a DataGridView and TextBox 20 52
run a stored procedure from vb.net 1 27
vb.net convert long time to mm:ss 23 12
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 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