Results 1 to 3 of 3

Thread: Numerical Listview Sorting

  1. #1

    Thread Starter
    Member
    Join Date
    May 2000
    Location
    Boston, MA
    Posts
    52

    Question

    Hello,

    I'm trying to display some data in a listview and one of the columns on which I wish to allow the user to sort will be filled with numerical data (e.g. 1202,323,566,etc). Here is the code I'm using right now:

    Private Sub lvwmain_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)

    If lvwmain.SortKey = ColumnHeader.Index - 1 Then
    If lvwmain.SortOrder = lvwAscending Then
    lvwmain.SortOrder = lvwDescending
    Else
    lvwmain.SortOrder = lvwAscending
    End If
    End If
    lvwmain.SortKey = ColumnHeader.Index - 1
    lvwmain.Sorted = True

    End Sub

    This works fine on the columns in which there is string data, but when I try to sort the numerical data, it puts the 1000's above the 200's for exampls

    Col1 Col2
    ---- ----
    Hank 1102
    John 233
    Phil 266
    Alex 299
    etc...

    How can I make this listview sort it so that the numbers are in the correct order? Thanks in advance!


  2. #2
    Lively Member
    Join Date
    Apr 2000
    Location
    Rafaela (Argentine)
    Posts
    107
    You have to align them to the right, so VB will sort them as strings. You may use the following, to align numbers to the right:

    Code:
    Right(CStr(x) & String(5, " "), 5)
    where x is the number, and 5 the width in characters. This way, you'll obtain:

    Code:
    Col1 Col2 
    ---- ----- 
    John   233 
    Phil   266 
    Alex   299 
    Hank  1102
    Hope it helps!

  3. #3

    Thread Starter
    Member
    Join Date
    May 2000
    Location
    Boston, MA
    Posts
    52

    Exclamation Makes Sence

    That makes sense! What I had been doing is adding a '0' in front of the number, but the " " is so much cleaner. I just made a function called FormatForList that does it for me...here's the code I came up with....

    Code:
    Public Function FormatForList(fflListItem As Integer, Length As Integer)
    
        Dim strPlace As String
        strPlace = Trim(Str(fflListItem))
        If Len(strPlace) = Length Then
            FormatForList = " " & strPlace
        Else
            FormatForList = strPlace
        End If
        
    End Function
    Where the fflListItem is the Numerical Value and the Length is the length of the number for which leading spaces must be added. I think I like your code better though... Thanks!!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width