Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-11-30
6
Medium Priority
?
229 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

688 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