Link to home
Create AccountLog in
Avatar of Seven price
Seven priceFlag for United States of America

asked on

treeview sorting childnodes

Dim rootDir As New DirectoryInfo(Server.MapPath("~/location/"))

                Dim d As DateTime
                Dim RootNode As TreeNode = RecurseNodes(rootDir)
                Treeview1.Nodes.Add(RootNode)
                SortTreeNodes(RootNode.Parent.ChildNodes)
                RootNode.SelectAction = TreeNodeSelectAction.None
                Dim subDirs As DirectoryInfo() = rootDir.GetDirectories()

trying to sort in descending order using code below. when I had 2 nodes the bottom node moved to the middle. but 3 nodes is a problem I cannot get it to move at all.


  Private Sub SortTreeNodes(treeNodes As TreeNodeCollection)
        Dim sorted = True

        For Each treeNode As TreeNode In treeNodes
            SortTreeNodes(treeNode.ChildNodes)
        Next

        Do
            sorted = True

            For i = 0 To treeNodes.Count - 1
                Dim treeNode1 = treeNodes(i)
                Dim treeNode2 = treeNodes(i)
                Dim treeNode3 = treeNodes(i + 1)

                ' If treeNode1.Text.CompareTo(treeNode3.Text) < 0 Then
                treeNodes.RemoveAt(i)
                treeNodes.RemoveAt(i)
                treeNodes.RemoveAt(i)

                treeNodes.AddAt(i, treeNode1)
                treeNodes.AddAt(i, treeNode2)
                treeNodes.AddAt(i, treeNode3)

                Response.Write(i)
                '   End If
            Next
        Loop While Not sorted
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Seven price

ASKER

Private Sub sort(ByVal node As TreeNode)
        For Each n As TreeNode In node.ChildNodes
            sort(n)
        Next

        Dim temp As TreeNode = Nothing
        Dim childs As List(Of TreeNode) = New List(Of TreeNode)

        While (node.ChildNodes.Count > 0)
            For Each n As TreeNode In node.ChildNodes
                If ((temp Is Nothing) _
                            OrElse (n.Text(0) < temp.Text(0))) Then
                    temp = n
                End If
            Next
            node.ChildNodes.Remove(temp)
            childs.Add(temp)
            temp = Nothing

        End While
        node.ChildNodes.Clear()
        For Each a As TreeNode In childs
            node.ChildNodes.Add(a)
        Next


    End Sub

Open in new window


I did that but I need to have it descending instead of ascending.
also in my page load I have
   Dim RootNode As TreeNode = RecurseNodes(rootDir)
            Treeview1.Nodes.Add(RootNode)
            sort(RootNode)

not sure it is working nothing seems to be effected.
thanks I got it.
  OrElse (n.Text(1) > temp.Text(0))) Then