Results 1 to 7 of 7

Thread: Can Listview sort

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Springfield
    Posts
    120

    Can Listview sort

    Is listview silly or just me?
    I use .SortOrder = lvwAscending, it works fine for string. But it does not sort properly for integer. It sorts like this
    11
    111
    22
    222
    3
    I know it because it treat integer as string, but is some way we can sort integer??? Thanks in advance
    MM

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    well

    Heres what I got from help.

    [code]

    Settings


    lvwAscending 0 (Default) Ascending order. Sorts from the beginning of the alphabet (A-Z) or the earliest date. Numbers are sorted as strings, with the first digit determining the initial position in the sort, and subsequent digits determining sub-sorting.
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    here what i use

    VB Code:
    1. Private Sub lstLocal_ColumnClick(ByVal ColumnHeader As MSComctlLib.ColumnHeader)
    2. Dim lngStart As Long
    3.     Dim lngCursor As Long
    4.     Dim l As Long
    5.     Dim strFormat As String
    6.     Dim strData() As String
    7.     Dim lngIndex As Long
    8.     On Error Resume Next
    9.    
    10.     With lstLocal
    11.         ' Display the hourglass cursor while list is sorting
    12.         lngCursor = .MousePointer
    13.         .MousePointer = vbHourglass
    14.        
    15.         ' Prevent the ListView control from updating on screen
    16.         ' and also to speed up the sort
    17.         LockWindowUpdate .hwnd
    18.        
    19.         ' Check the data type of the column being sorted,
    20.         lngIndex = ColumnHeader.Index - 1
    21.    
    22.         Select Case UCase$(ColumnHeader.Tag)
    23.             Case "DATE"
    24.                 ' Sort by date.
    25.                 strFormat = "YYYYMMDDHhNnSs"
    26.                 ' Loop through the values in this column. Re-format
    27.                 ' the dates so as they can be sorted alphabetically,
    28.                 With .ListItems
    29.                     If (lngIndex > 0) Then
    30.                         For l = 1 To .Count
    31.                             With .Item(l).ListSubItems(lngIndex)
    32.                                 .Tag = .Text & Chr$(0) & .Tag
    33.                                 If IsDate(.Text) Then
    34.                                     .Text = Format(CDate(.Text), _
    35.                                     strFormat)
    36.                                 Else
    37.                                     .Text = ""
    38.                                 End If
    39.                             End With
    40.                         Next l
    41.                     Else
    42.                         For l = 1 To .Count
    43.                             With .Item(l)
    44.                                 .Tag = .Text & Chr$(0) & .Tag
    45.                                 If IsDate(.Text) Then
    46.                                     .Text = Format(CDate(.Text), _
    47.                                     strFormat)
    48.                                 Else
    49.                                     .Text = ""
    50.                                 End If
    51.                             End With
    52.                         Next l
    53.                     End If
    54.                 End With
    55.                 ' Sort the list alphabetically by this column
    56.                 .SortOrder = (.SortOrder + 1) Mod 2
    57.                 .SortKey = ColumnHeader.Index - 1
    58.                 .Sorted = True
    59.                
    60.                 ' Restore the previous values to the 'cells' in this
    61.                 ' column of the list from the tags, and also restore
    62.                 ' the tags to their original values
    63.                 With .ListItems
    64.                     If (lngIndex > 0) Then
    65.                         For l = 1 To .Count
    66.                             With .Item(l).ListSubItems(lngIndex)
    67.                                 strData = Split(.Tag, Chr$(0))
    68.                                 .Text = strData(0)
    69.                                 .Tag = strData(1)
    70.                             End With
    71.                         Next l
    72.                     Else
    73.                         For l = 1 To .Count
    74.                             With .Item(l)
    75.                                 strData = Split(.Tag, Chr$(0))
    76.                                 .Text = strData(0)
    77.                                 .Tag = strData(1)
    78.                             End With
    79.                         Next l
    80.                     End If
    81.                 End With
    82.            
    83.             Case "NUMBER"
    84.                 ' Sort Numerically
    85.                 strFormat = String(30, "0") & "." & String(30, "0")
    86.                 'Re-format the values so as they
    87.                 ' can be sorted alphabetically.
    88.                 With .ListItems
    89.                     If (lngIndex > 0) Then
    90.                         For l = 1 To .Count
    91.                             With .Item(l).ListSubItems(lngIndex)
    92.                                 .Tag = .Text & Chr$(0) & .Tag
    93.                                 If IsNumeric(.Text) Then
    94.                                     If CDbl(.Text) >= 0 Then
    95.                                         .Text = Format(CDbl(.Text), _
    96.                                         strFormat)
    97.                                     Else
    98.                                         .Text = "&" & InvNumber( _
    99.                                         Format(0 - CDbl(.Text), _
    100.                                         strFormat))
    101.                                     End If
    102.                                 Else
    103.                                     .Text = ""
    104.                                 End If
    105.                             End With
    106.                         Next l
    107.                     Else
    108.                         For l = 1 To .Count
    109.                             With .Item(l)
    110.                                 .Tag = .Text & Chr$(0) & .Tag
    111.                                 If IsNumeric(.Text) Then
    112.                                     If CDbl(.Text) >= 0 Then
    113.                                         .Text = Format(CDbl(.Text), _
    114.                                         strFormat)
    115.                                     Else
    116.                                         .Text = "&" & InvNumber( _
    117.                                         Format(0 - CDbl(.Text), _
    118.                                         strFormat))
    119.                                     End If
    120.                                 Else
    121.                                     .Text = ""
    122.                                 End If
    123.                             End With
    124.                         Next l
    125.                     End If
    126.                 End With
    127.                 ' Sort the list alphabetically by this column
    128.                 .SortOrder = (.SortOrder + 1) Mod 2
    129.                 .SortKey = ColumnHeader.Index - 1
    130.                 .Sorted = True
    131.            
    132.                 ' Restore the previous values to the 'cells' in this
    133.                 ' column of the list from the tags, and also restore
    134.                 ' the tags to their original values
    135.                 With .ListItems
    136.                     If (lngIndex > 0) Then
    137.                         For l = 1 To .Count
    138.                             With .Item(l).ListSubItems(lngIndex)
    139.                                 strData = Split(.Tag, Chr$(0))
    140.                                 .Text = strData(0)
    141.                                 .Tag = strData(1)
    142.                             End With
    143.                         Next l
    144.                     Else
    145.                         For l = 1 To .Count
    146.                             With .Item(l)
    147.                                 strData = Split(.Tag, Chr$(0))
    148.                                 .Text = strData(0)
    149.                                 .Tag = strData(1)
    150.                             End With
    151.                         Next l
    152.                     End If
    153.                 End With
    154.             Case Else
    155.                 'Just sort it alphabetically
    156.                 .SortOrder = (.SortOrder + 1) Mod 2
    157.                 .SortKey = ColumnHeader.Index - 1
    158.                 .Sorted = True
    159.         End Select
    160.         ' Unlock the list window so that the OCX can update it
    161.         LockWindowUpdate 0&
    162.         ' Restore the previous cursor
    163.         .MousePointer = lngCursor
    164.     End With
    165.  
    166. End Sub

    just put a tag to each column:
    TEXT,NUMBER or DATE

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Springfield
    Posts
    120
    Thanks very much, I will try this.
    MM

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Springfield
    Posts
    120
    Thanks very much, but there is a function called InvNumber, What is that? call you explain? Thanks very much
    MM

  6. #6
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    sorry i forgot:

    VB Code:
    1. Public Function InvNumber(ByVal Number As String) As String
    2.     'This function is to deal with the negative numbers, so then they can be sorted alphabetically
    3.  
    4.     Static i As Integer
    5.     For i = 1 To Len(Number)
    6.         Select Case Mid$(Number, i, 1)
    7.         Case "-": Mid$(Number, i, 1) = " "
    8.         Case "0": Mid$(Number, i, 1) = "9"
    9.         Case "1": Mid$(Number, i, 1) = "8"
    10.         Case "2": Mid$(Number, i, 1) = "7"
    11.         Case "3": Mid$(Number, i, 1) = "6"
    12.         Case "4": Mid$(Number, i, 1) = "5"
    13.         Case "5": Mid$(Number, i, 1) = "4"
    14.         Case "6": Mid$(Number, i, 1) = "3"
    15.         Case "7": Mid$(Number, i, 1) = "2"
    16.         Case "8": Mid$(Number, i, 1) = "1"
    17.         Case "9": Mid$(Number, i, 1) = "0"
    18.         End Select
    19.     Next
    20.     InvNumber = Number
    21. End Function

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Springfield
    Posts
    120
    The code is working great, thanks very very much
    MM

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