Re: Sorting a Listview acting as treeview (topmost parent only)
I have initially thought of that method but that will work if the items are already sorted either ascendingly or descendingly but will not be 100% accurate if the items are not yet sorted since the first two items can be sorted and yet the third and succeeding ones can be unsorted.
Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)
Well done.
Your ArrayOrder() function could be fooled by an initial random order where incidentally the first 3 elements are in ascending order.
But never mind. It works very good as it is.
Also the quickort is much more sophisticated than my little bubbler.
I remember, there was a ListView hack with usage of SendMessage() which put little icons of up-arrow or downarrow into the columnheader, thus signalling if the column was sorted ascending or descending. I shall try to find this again. Could be helpful.
Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)
Ok I found this:
Maybe it is worth a look. By adding the up-arrow or down-arrow to a column header you have got a sure flag which order you got. So you can determine easily which order you have to sort when clicking the header.
Could be a nice extension to your little program.
Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)
Originally Posted by WoF
Your ArrayOrder() function could be fooled by an initial random order where incidentally the first 3 elements are in ascending order.
Hmmmnnn... Could you demonstrate such scenario? I tried with the sample data below and it worked still.
Code:
Private Sub Command1_Click()
Dim data(5) As String
data(0) = "Item11"
data(1) = "Item12"
data(2) = "Item13"
data(3) = "Item1"
data(4) = "Item3"
data(5) = "Item2"
MsgBox ArrayOrder(data)
End Sub
Public Function ArrayOrder(pvarArray As Variant) As Long
Dim lngDirection As Long
Dim i As Long
If Not IsArray(pvarArray) Then Exit Function
For i = LBound(pvarArray) To UBound(pvarArray) - 1
Select Case lngDirection
Case 1: If pvarArray(i) > pvarArray(i + 1) Then Exit Function
Case -1: If pvarArray(i) < pvarArray(i + 1) Then Exit Function
Case Else
If pvarArray(i) < pvarArray(i + 1) Then
lngDirection = 1
ElseIf pvarArray(i) > pvarArray(i + 1) Then
lngDirection = -1
End If
End Select
Next
ArrayOrder = lngDirection
End Function
Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)
Originally Posted by WoF
I remember, there was a ListView hack with usage of SendMessage() which put little icons of up-arrow or downarrow into the columnheader, thus signalling if the column was sorted ascending or descending. I shall try to find this again. Could be helpful.
Thank you for this one but then again, what if new unsorted rows are added to the listview? In effect such will break the symbol.
Re: [RESOLVED] Sorting a Listview acting as treeview (topmost parent only)
@David: Right, but the sample uses an unsorted LV because of the unorthodox tree structure it can take. So no automagically sorting in of new items.
@Dee-U, you are right about the flag thing. Maybe you better update the arrow symbols according the actual sort order.
What concerns your ArrayOrder() function.
I thought it was supposed to return 0 for an unsorted array, -1 for a descending and +1 for an ascending order.
But it returns 0 for an unsorted AND for an ascending array.
Try
I thought this should produce 1, signalling an ascending order, but it comes out 0.
It produces the -1 for descending arrays, though.
And I have to apology: it is NOT fooled by a random sequence as I thought in the first place.
But it is fooled by a properly sorted array.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.