sorting of filelist

fdsdesmet

New Member
Licensed User
Longtime User
Can anyone confirm that the standard sorting of file.listfiles is on date modified decending. I looks like it, but i can not find documentation about this.

Thanks,
Edwin
 

Smee

Well-Known Member
Licensed User
Longtime User
The files are not ordered in any specific way. You will need to sort them yourself. If you need help with it I can show you some code that will sort the list based on the last modified attribute.

Is this code available Erel?

TIA
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Process_Globals
   Type FileAndTime(Name As String, Time As Long)
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim files As List
   files = ListFilesByDate(File.DirRootExternal)
   For i = 0 To files.Size - 1
      Dim fs As FileAndTime
      fs = files.Get(i)
      Log(fs.Name & ": " & DateTime.Date(fs.Time))
   Next
End Sub


Sub ListFilesByDate(Folder As String) As List
   Dim files As List
   files = File.ListFiles(Folder)
   Dim sortedFiles As List
   sortedFiles.Initialize
   For i = 0 To files.Size - 1
      Dim fs As FileAndTime
      fs.Name = files.Get(i)
      fs.Time = File.LastModified(Folder, fs.Name)
      sortedFiles.Add(fs)
   Next
   sortedFiles.SortType("Time", False)
   Return sortedFiles
End Sub
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Hello Erel,
I Finally got around to using this code. I got the following error when compiling

B4X:
Compiling code.                         Error
Error compiling program.
Error description: Unknown member: sorttype
Occurred on line: 367
    sortedFiles.SortType("Time", False)
Word: sorttype
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
I have re-compiled under 1.9 but i get an error

Compiling code. 0.78
Compiling layouts code. 0.00
Generating R file. 0.00
Compiling generated Java code. Error
The directory name is invalid

paths as follows

E:\Java SDK\bin\javac.exe
E:\Android\Tools\android-sdk\platforms\android-8\android.jar
E:\Basic4Android\DeveloperLibraries

exe is here

E:\Basic4AndroidV1.9

Can you see where the problem is?
 
Upvote 0

Smee

Well-Known Member
Licensed User
Longtime User
Thanks Erel that fixed it. But the previous version had spaces in the path and it was fine
 
Upvote 0

pcbtmr

Member
Licensed User
Longtime User
File List Sorting

Erel,
Given this code:
fileList=File.ListFiles(DataFolder)
sortedFileList=fileList.Sort(False)

compiler says "cannot assign void value" flagging the last line. Don't know what I'm missing. Both variables are defined as lists.

Thanks!
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Are you sure that there are files in the DataFolder folder?
You could use following code
B4X:
fileList=File.ListFiles(DataFolder)
If fileList.Size > 0 then 
  sortedFileList=fileList.Sort(False)
Else
  MsgBox("No file", "File read")
End If
Best regards.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Erel,
Given this code:
fileList=File.ListFiles(DataFolder)
sortedFileList=fileList.Sort(False)

compiler says "cannot assign void value" flagging the last line. Don't know what I'm missing. Both variables are defined as lists.

Thanks!
I think something is wrong here. Could you try this?
B4X:
fileList=File.ListFiles(DataFolder)
fileList.sort(false)
sortedFileList.addall(fileList)
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Or perhaps this:
B4X:
fileList=File.ListFiles(DataFolder)
fileList.sort(False)
sortedFileList = fileList
But do you really need a second list with the content of the sorted list ?
After fileList.sort(False), fileList is sorted.

Best regards.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Or perhaps this:
B4X:
fileList=File.ListFiles(DataFolder)
fileList.sort(False)
sortedFileList = fileList
But do you really need a second list with the content of the sorted list ?
After fileList.sort(False), fileList is sorted.

Best regards.

I think, what he wanted to achieve, was to leave the first list unsorted, and sort data to the second list. Just a thought :)
 
Upvote 0

pcbtmr

Member
Licensed User
Longtime User
Yahoo! Actually I don't need the second sortedFileList, only as a repository of the action of sorting. I realize that the sorting actually changes the FileList contents.
Many thanks for the assistance!
 
Upvote 0
Top