Android Question listfiles is returning file names out of order

chuck3e

Active Member
Licensed User
Longtime User
No need to answer this question at this time. I found an example of a sort module in the beginners guide I will experiment with. I would like to know why listfiles doesn't normally return files in filename sequence, though.

I'm using the command listfiles to return the filenames of all the files in a directory(folder). The file names include the date it was created. All of the file names are identical except for the date. The problem is that the files names are not always returned in date sequence. Is there a way to get listfiles to sort by name? If not is there an example of how to efficiently sort and array?

I'm using:

B4X:
Public MyList As List
Public MyFile As String

MyList.Initialize
MyList = File.ListFiles(File.DirRootExternal & "/ELSIE/")
MyFile = MyList.Get(0)

This was working fine until today. Now the file I created today is first in the list instead of last.
 
Last edited:

tunderin

Member
Licensed User
Longtime User
You could try Erel's ListFilesByDate sub found in this post
 
Upvote 0

chuck3e

Active Member
Licensed User
Longtime User
Tunderin, Thank you for your quick reply. That is the example I found in the beginner's guide. I have tested it and it does what I was looking for. Actually it did more because I have been wanting to sort the names in descending order, too.

Thanks again!!!
-Chuck
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
it did more because I have been wanting to sort the names in descending order, too.

Have you tried
B4X:
filelist.Sort(True) 'sorts ascending the list
with the list returned by
B4X:
File.ListFiles
?
 
Upvote 0

chuck3e

Active Member
Licensed User
Longtime User
DonManfred, thank you very much. This made it so much more easy and I like the returned format better.

Here is all I had to do to get the list, get it sorted, and get the first filename.

B4X:
Dim MyList   As List
Dim MyFile   As String
MyList.Initialize   
MyList = File.ListFiles(File.DirRootExternal & "/ELSIE/")
MyList.Sort(True) 'sorts ascending the list
MyFile=MyList.Get(0)

Thanks again so much!!!
-C
 
Upvote 0
Top