B4J Question Sort TreeViewItem

GMan

Well-Known Member
Licensed User
Longtime User
Is it possible to sort the putput of a treeview that shows directorys ?

Here is the code thats belong to that:

B4X:
Sub recursiveFolder(tr As TreeItem,folder As String)
    Dim l As List = getFolders(folder)

    If l.Size = 0 Then
        Dim tr2 As TreeItem
        tr2.Initialize("","{Empty}")
        tr.Children.Add(tr2)
        Return
    End If

    For i = 0 To l.Size - 1
        Dim tr2 As TreeItem
        tr2.Initialize("tr",l.Get(i))
        tr.Children.Add(tr2)
        If File.IsDirectory(folder,l.Get(i)) Then
            recursiveFolder(tr2,folder&"\"&l.Get(i))
        End If
    Next
End Sub

private Sub getFolders(folder As String) As List
    Dim l As List = File.ListFiles(folder)
    Dim lreturn As List
    lreturn.Initialize
    For i = 0 To l.size - 1
        Dim fName As String = l.Get(i)
'        If File.IsDirectory(folder,fName) Then
        lreturn.Add(fName)
'        End If
    Next

    Return lreturn
End Sub

I followed Erel Tut but found no answer or hint
 

GMan

Well-Known Member
Licensed User
Longtime User
This did it:
B4X:
    Dim l As List = File.ListFiles(folder)
    l.Sort(True)  'Erel's hint
    Dim lreturn As List
.etc
But: the list is i.e. ....8,9,10,100,1000,110,12,120,......

How can i solve this so the lst shows the "real" sorting ...8,9,10,11,12,13,......100,120 etc.

"Playing" with the "Sort MultilineString" from the B4J HowTo doesnt change anything:

B4X:
' Parameter: The String to sort
' Returns String ASC
Sub sortMultiLineString(mls As String) As String
  If mls.Length = 0 Then Return ""
  ' Split the text
  Dim s() As String = Regex.Split(CRLF, mls)
  Dim l As List
  l.Initialize
  l.AddAll(s)
  l.sort(True)
  mls = ""
  For i = 0 To s.Length - 1
      mls = mls & l.Get(i) & CRLF
  Next
  Return mls
End Sub

Example sorting Form TextArea
Private ta As TextArea
ta.Text = sortMultiLineString(ta.Text)

Adapted it to this:

B4X:
private Sub getFolders(folder As String) As List
    Dim l As List = File.ListFiles(folder)
    ' SORTIEREN
    l.Sort(True)  'Erel's hint
    Dim lreturn As List
    lreturn.Initialize
    For i = 0 To l.size - 1
        Dim fName As String = l.Get(i)
        fName = sortMultiLineString(fName) ' myTry 

'        If File.IsDirectory(folder,fName) Then
        lreturn.Add(fName)
'        End If
    Next
    Return lreturn
End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…