Results 1 to 8 of 8

Thread: Sorting ListBox Items ?

  1. #1
    ashky
    Guest

    Question Sorting ListBox Items ?

    Hai,

    I am having a query with the .Sorted property of a ListBox Control. I am aware that it can be set only during Design time.

    But what if i wanted to sort only according to the last column of the string in the listbox.

    Example:
    ========

    Let's say these rows are added to the List1 already.
    VB Code:
    1. 101000010100 4
    2.       000001001010 3
    3.       110100110000 5
    4.       010000000000 1
    5.       100101100000 4
    6.       101000000000 2

    Where the last column value corresponds to the rowsum of each string. I compute the rowsum as i create the string.

    With the below code
    VB Code:
    1. List1.AddItem StringRow & Chr(9) & RowSum
    Now how can i sort according to the last column. I set the sorted property to true, but this is sorting by Value and give wrong results, while i wanted to sorted according to the RowSums. (the last column)


    Any codes/ ideas / highly appreciated ,

    ashky.

  2. #2
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Reverse the columns or use a ListView control instead.
    -Excalibur

  3. #3
    ashky
    Guest

    Question What is reversing the column ?

    what did u mean by reversing the columns ?

    please clarify ?

    thanks for the reply ...,


    any ideas/ codes highly appreciated..,

    ashky.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Where are you getting the data from?

    If it is coming from a table, and the List box is being populated by a recordset created from an SQL call, you could sort the recordset first, then populate the List box.

  5. #5
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Reverse the columns means exactly that. Put the sum before the other part if you want to sort by the sum. can't do it any other way that I'm aware of (sort works from left to right) If you want to sort by different "TRUE" columns (your way of filling the listbox is not a true column) use the listview control instead.
    -Excalibur

  6. #6
    ashky
    Guest

    Question ListView ?

    Actually i do not get data from database or something. I create them one by one like this

    VB Code:
    1. If ProductSum <= Bound Then
    2.         If strrow = "" Then
    3.           strrow = "0"
    4.           strrow1 = "0"
    5.         Else
    6.           strrow = strAddrow & Chr(9) & "0"
    7.           strrow1 = strAddrow1 & "0"
    8.         End If
    9.      Else
    10.         If strrow = "" Then
    11.           strrow = "1"
    12.           strrow1 = "1"
    13.         Else
    14.            strrow = strAddrow & Chr(9) & "1"
    15.            strrow1 = strAddrow1 & "1"
    16.         End If
    17.            intSum = intSum + 1
    18.      End If

    And then do the additem step. By the way, there is "NO" .Additem property in ListView then , how will i add my string ?????????

    I have using MSFlexGrid for this, but encountered some errors like, "Unable to allocate memory to MSFlexGrid" , that is is the reason i started trying List items, but this is also having some disadvantages ?????????????

    thanks for the replies ....,

    still could not figure out how to sort, coz, in FleGrid, it was simple ,,
    VB Code:
    1. Like .Col = .Cols -1
    2.              .Sort = flexSortNumericAscending

  7. #7
    spetnik
    Guest
    here is an example of a merge sort:

    VB Code:
    1. Public Function MergeSort(Strings() As String) As String()
    2.    
    3.     Dim s As Long, i As Long, n() As String, m() As String
    4.     Dim u As Long, l As Long, j As Long, x() As String
    5.    
    6.     ' we don't sort 1 element!
    7.    
    8.     If (UBound(Strings) - LBound(Strings)) = 0 Then
    9.         MergeSort = Strings
    10.         Exit Function
    11.     ElseIf (UBound(Strings) - LBound(Strings)) = 1 Then
    12.         MergeSort = BubbleSort(Strings)
    13.         Exit Function
    14.     Else
    15.         l = SplitArray(Strings, m, n)
    16.         m = MergeSort(m)
    17.         n = MergeSort(n)
    18.         MergeSort = MergeArray(m, n)
    19.     End If
    20.        
    21. End Function
    22.  
    23. Private Function SplitArray(Strings() As String, _
    24.    StringOut1() As String, StringOut2() As String) As Long
    25.  
    26. ' Splits it 50/50 or as close as possible
    27. ' if it's just one string, we return StringOut1(0)
    28. ' The return number is the total elements of the
    29. ' largest of the arrays
    30.  
    31.     Dim i As Long, j As Long, s1() As String, s2() As String
    32.     Dim z As Long
    33.  
    34.     If (UBound(Strings) - LBound(Strings)) = 0 Then
    35.         StringOut1 = Strings
    36.         SplitArray = 1
    37.         Exit Function
    38.     End If
    39.    
    40.     i = Int((UBound(Strings) + 1) / 2)
    41.    
    42.     z = 0
    43.     For j = 0 To (i - 1)
    44.         ReDim Preserve StringOut1(z)
    45.         StringOut1(z) = Strings(j)
    46.         z = z + 1
    47.     Next j
    48.    
    49.     z = 0
    50.     For j = i To UBound(Strings)
    51.         ReDim Preserve StringOut2(z)
    52.         StringOut2(z) = Strings(j)
    53.         z = z + 1
    54.     Next j
    55.  
    56.     If UBound(StringOut1) > UBound(StringOut2) Then
    57.         SplitArray = UBound(StringOut1)
    58.     Else
    59.         SplitArray = UBound(StringOut2)
    60.     End If
    61.    
    62. End Function
    63.  
    64. Private Function MergeArray(String1() As String, _
    65.    String2() As String) As String()
    66.  
    67.     Dim i As Long, j As Long
    68.     Dim n() As String, x As Long, y As Long, c As Integer
    69.     On Error Resume Next
    70.     i = -2
    71.     j = -2
    72.    
    73.     i = UBound(String1) + 1
    74.     j = UBound(String2) + 1
    75.    
    76.     If (i < 0) And (j < 0) Then Exit Function
    77.    
    78.     If (i > -1) Then
    79.         i = UBound(String1) + 1
    80.     ElseIf (i = -1) Or ((i = 0) And (String1(0) = "")) Then
    81.         MergeArray = String2
    82.         Exit Function
    83.     End If
    84.    
    85.     If j > -1 Then
    86.         i = i + UBound(String2) + 1
    87.     ElseIf (j = -1) Or ((j = 0) And (String2(0) = "")) Then
    88.         MergeArray = String1
    89.         Exit Function
    90.     End If
    91.    
    92.     ReDim n(i - 1)
    93.    
    94.     For j = 0 To (i - 1) Step 0
    95.         If (x > UBound(String1)) And (y > UBound(String2)) Then
    96.             MergeArray = n
    97.             Exit Function
    98.         End If
    99.        
    100.         c = StrComp(String1(x), String2(y))
    101.         If (c = 0) And (x <= UBound(String1) And _
    102.               (y <= UBound(String2))) Then
    103.             n(j) = String1(x)
    104.             n(j + 1) = String2(y)
    105.             j = j + 2
    106.             y = y + 1
    107.             x = x + 1
    108.         ElseIf ((c < 0) Or (y > UBound(String2))) _
    109.               And (x <= UBound(String1)) Then
    110.             n(j) = String1(x)
    111.             x = x + 1
    112.             j = j + 1
    113.         ElseIf ((c > 0) Or (x > UBound(String1))) _
    114.                 And (y <= UBound(String2)) Then
    115.             n(j) = String2(y)
    116.             y = y + 1
    117.             j = j + 1
    118.         End If
    119.     Next
    120.     MergeArray = n
    121.    
    122. End Function
    123.  
    124. Public Function BubbleSort(Strings() As String) As String()
    125.  
    126.     Dim a As Long, b As Long, c As Long, d As Long
    127.     Dim e As Integer, f As Integer, g As Integer
    128.  
    129.     Dim i As String, j As String
    130.     Dim m() As String, n() As String
    131.    
    132.     e = 1
    133.     n = Strings
    134.     Do While e <> -1
    135.    
    136.         For a = 0 To UBound(Strings) - 1
    137.             i = n(a)
    138.             j = n(a + 1)
    139.             f = StrComp(i, j)
    140.             If f <= 0 Then
    141.                 n(a) = i
    142.                 n(a + 1) = j
    143.             Else
    144.                 n(a) = j
    145.                 n(a + 1) = i
    146.                 g = 1
    147.             End If
    148.         Next a
    149.         If g = 1 Then
    150.             e = 1
    151.         Else
    152.             e = -1
    153.         End If
    154.        
    155.         g = 0
    156.     Loop
    157.     BubbleSort = n
    158. End Function

    if u use this, set the itemdata property of the listbox to the value of the second column. then, u can run the merge sort on the itemdata property.

  8. #8
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi ashky
    I'm sure u can adapt the code i posted on this thread which does a very similar thing to what u are after.
    http://www.vbforums.com/showthread.p...threadid=96157
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

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