|
-
Sep 10th, 2001, 12:12 PM
#1
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:
101000010100 4
000001001010 3
110100110000 5
010000000000 1
100101100000 4
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:
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.
-
Sep 10th, 2001, 12:14 PM
#2
Fanatic Member
Reverse the columns or use a ListView control instead.
-
Sep 10th, 2001, 12:18 PM
#3
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.
-
Sep 10th, 2001, 12:19 PM
#4
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.
-
Sep 10th, 2001, 12:24 PM
#5
Fanatic Member
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.
-
Sep 10th, 2001, 12:37 PM
#6
ListView ?
Actually i do not get data from database or something. I create them one by one like this
VB Code:
If ProductSum <= Bound Then
If strrow = "" Then
strrow = "0"
strrow1 = "0"
Else
strrow = strAddrow & Chr(9) & "0"
strrow1 = strAddrow1 & "0"
End If
Else
If strrow = "" Then
strrow = "1"
strrow1 = "1"
Else
strrow = strAddrow & Chr(9) & "1"
strrow1 = strAddrow1 & "1"
End If
intSum = intSum + 1
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:
Like .Col = .Cols -1
.Sort = flexSortNumericAscending
-
Sep 10th, 2001, 12:41 PM
#7
here is an example of a merge sort:
VB Code:
Public Function MergeSort(Strings() As String) As String()
Dim s As Long, i As Long, n() As String, m() As String
Dim u As Long, l As Long, j As Long, x() As String
' we don't sort 1 element!
If (UBound(Strings) - LBound(Strings)) = 0 Then
MergeSort = Strings
Exit Function
ElseIf (UBound(Strings) - LBound(Strings)) = 1 Then
MergeSort = BubbleSort(Strings)
Exit Function
Else
l = SplitArray(Strings, m, n)
m = MergeSort(m)
n = MergeSort(n)
MergeSort = MergeArray(m, n)
End If
End Function
Private Function SplitArray(Strings() As String, _
StringOut1() As String, StringOut2() As String) As Long
' Splits it 50/50 or as close as possible
' if it's just one string, we return StringOut1(0)
' The return number is the total elements of the
' largest of the arrays
Dim i As Long, j As Long, s1() As String, s2() As String
Dim z As Long
If (UBound(Strings) - LBound(Strings)) = 0 Then
StringOut1 = Strings
SplitArray = 1
Exit Function
End If
i = Int((UBound(Strings) + 1) / 2)
z = 0
For j = 0 To (i - 1)
ReDim Preserve StringOut1(z)
StringOut1(z) = Strings(j)
z = z + 1
Next j
z = 0
For j = i To UBound(Strings)
ReDim Preserve StringOut2(z)
StringOut2(z) = Strings(j)
z = z + 1
Next j
If UBound(StringOut1) > UBound(StringOut2) Then
SplitArray = UBound(StringOut1)
Else
SplitArray = UBound(StringOut2)
End If
End Function
Private Function MergeArray(String1() As String, _
String2() As String) As String()
Dim i As Long, j As Long
Dim n() As String, x As Long, y As Long, c As Integer
On Error Resume Next
i = -2
j = -2
i = UBound(String1) + 1
j = UBound(String2) + 1
If (i < 0) And (j < 0) Then Exit Function
If (i > -1) Then
i = UBound(String1) + 1
ElseIf (i = -1) Or ((i = 0) And (String1(0) = "")) Then
MergeArray = String2
Exit Function
End If
If j > -1 Then
i = i + UBound(String2) + 1
ElseIf (j = -1) Or ((j = 0) And (String2(0) = "")) Then
MergeArray = String1
Exit Function
End If
ReDim n(i - 1)
For j = 0 To (i - 1) Step 0
If (x > UBound(String1)) And (y > UBound(String2)) Then
MergeArray = n
Exit Function
End If
c = StrComp(String1(x), String2(y))
If (c = 0) And (x <= UBound(String1) And _
(y <= UBound(String2))) Then
n(j) = String1(x)
n(j + 1) = String2(y)
j = j + 2
y = y + 1
x = x + 1
ElseIf ((c < 0) Or (y > UBound(String2))) _
And (x <= UBound(String1)) Then
n(j) = String1(x)
x = x + 1
j = j + 1
ElseIf ((c > 0) Or (x > UBound(String1))) _
And (y <= UBound(String2)) Then
n(j) = String2(y)
y = y + 1
j = j + 1
End If
Next
MergeArray = n
End Function
Public Function BubbleSort(Strings() As String) As String()
Dim a As Long, b As Long, c As Long, d As Long
Dim e As Integer, f As Integer, g As Integer
Dim i As String, j As String
Dim m() As String, n() As String
e = 1
n = Strings
Do While e <> -1
For a = 0 To UBound(Strings) - 1
i = n(a)
j = n(a + 1)
f = StrComp(i, j)
If f <= 0 Then
n(a) = i
n(a + 1) = j
Else
n(a) = j
n(a + 1) = i
g = 1
End If
Next a
If g = 1 Then
e = 1
Else
e = -1
End If
g = 0
Loop
BubbleSort = n
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.
-
Sep 10th, 2001, 08:39 PM
#8
PowerPoster
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|