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

asked on

treeview sort by date

my below sorts by the first 2 numbers i guest in the child node folder.
problem I need it sort by the year

my folder child node example is like. 02122013
but if I have a date like 12252012 then the 12 comes before the 02

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

                Dim d As DateTime
                Dim RootNode As TreeNode = RecurseNodes(rootDir)
                Treeview1.Nodes.Add(RootNode)

                RootNode.SelectAction = TreeNodeSelectAction.None
                Dim subDirs As DirectoryInfo() = rootDir.GetDirectories()
                sort(RootNode)

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(1) > 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

Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Is the node text containing the date string?

IF so. You have two choices:
1) Fix the code that populates the nodes to the same date time format. My prefered solution because your user will have a clear understanding of what the node information is about.
2) Less prefered solution hard for user as they have to think what the format is and the sort will not make sense. Change if logic to convert text to date time and then compare date times.
Avatar of Seven price

ASKER

I did that already the childnodes are converted to date and time.  So were else but the sort not sure
So should I try a compare inn my sort if so
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
that does not seem to work. The sort comes after the reading of the files.
the compare is already done in the sort. as you see in the Function

 For Each subDir As DirectoryInfo In subDirs
            Dim d As DateTime
            Dim provider As IFormatProvider = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat

            If (DateTime.TryParseExact(subDir.Name, "MMddyyyy", provider, DateTimeStyles.None, d)) Then
                Dim subDirs2 As String = d.ToString("MMMM dd, yyyy")

                thisDirNode.ChildNodes.Add(RecurseNodes(subDir))
                thisDirNode.SelectAction = TreeNodeSelectAction.None
            End If

            If subDir.ToString = Request.QueryString("StoreId") Then
                thisDirNode.ChildNodes.Add(RecurseNodes(subDir))
                '   thisDirNode.ChildNodes(0).Text = subDir.Name
                thisDirNode.SelectAction = TreeNodeSelectAction.None
            End If
        Next

Open in new window


I change the format in the string still nothing.
It has to happen here some how I think.

If ((temp Is Nothing) _
                            OrElse (n.Text(1) > temp.Text(0))) Then
                    temp = n


                End If

Open in new window

I tryied your code again it does not convert well in vb i have to try something else
Here it is but did not work. feel like we are close

  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(1) > temp.Text(0))) Then
                    temp = n


                End If
                Dim d As DateTime
                Dim d2 As DateTime
                Dim result0 As String
                Dim result1 As String
                Dim provider As IFormatProvider = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat
                DateTime.TryParseExact(n.Text(1), "MMddyyyy", provider, DateTimeStyles.None, d)
                DateTime.TryParseExact(temp.Text(0), "MMddyyyy", provider, DateTimeStyles.None, d)
                Dim subDirs2 As String = d.ToString("MMMM dd, yyyy")
                result0 = d2.ToString("MMMM dd, yyyy")
                result1 = d.ToString("MMMM dd, yyyy")
                If (result0 < result1) 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

Open in new window

the concept helped and the turning of the dates also helped. Thanks