Android Question File.ListFiles sorted by size

ac9ts

Active Member
Licensed User
Longtime User
Is there a way to use File.ListFiles to read a directory sorted by file size or would I have to read the directory and look up and then sort by size?
 

Eme Fibonacci

Well-Known Member
Licensed User
Longtime User
Maybe something like this:

B4X:
'ON Process_Globals
 Type NameAndSize(name As String, size As Long)
'...
Dim list1 As List
list1=File.ListFiles(File.DirInternal&"/mydir")
  
Dim list2 As List :list2.Initialize
For i=0 To list1.Size-1
     Dim fns As NameAndSize : fns.Initialize
     fns.name=list1.Get(i)
     fns.size=File.Size(File.DirInternal,"mydir/"&fns.name)
     list2.Add(fns)
Next

list2.SortType("size",False)

For i=0 To list2.Size-1
     Dim fns As NameAndSize=list2.Get(i)
     Log("name:" & fns.name & " size:" &fns.size)
Next
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
Yep. That's pretty much (almost line per line) of what I came up with. I was just wondering if there was a way built into Android or B4x. Thanks.
 
Upvote 0
Top