Solved

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

Posted on 2006-11-30
6
220 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 142

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 142

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 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

9 Experts available now in Live!

Get 1:1 Help Now