Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-11-30
6
Medium Priority
?
234 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline

971 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