How to sort the following list?
-
How to sort the following list, nodes is my list tv is my treeview Now I'd like nodes to sort.
Dim nodes As New List(Of TreeNode)
Dim queue As New Queue(Of TreeNode)
Dim top As TreeNode
Dim nod As TreeNode
For Each top In Tv.Nodes
queue.Enqueue(top)
Next
While (queue.Count > 0)
top = queue.Dequeue
nodes.Add(top)
For Each nod In top.Nodes
queue.Enqueue(nod)
Next
End WhileThanks!
-
How to sort the following list, nodes is my list tv is my treeview Now I'd like nodes to sort.
Dim nodes As New List(Of TreeNode)
Dim queue As New Queue(Of TreeNode)
Dim top As TreeNode
Dim nod As TreeNode
For Each top In Tv.Nodes
queue.Enqueue(top)
Next
While (queue.Count > 0)
top = queue.Dequeue
nodes.Add(top)
For Each nod In top.Nodes
queue.Enqueue(nod)
Next
End WhileThanks!
Why do you use a Queue? the main purpose of a queue is to act as a pipe where things get added on one side, stored, and removed on the other side, which inherently means their order is always preserved. What you need is another kind of collection, such as a simple List, on which you can call Sort(). Then provide a Comparer to explain how you compare two TreeNode instances. This[^] explains and illustrates it. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages