sorting of filelist

fdsdesmet

New Member
Licensed 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
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
 

Erel

Administrator
Staff member
Licensed 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
 

Smee

Well-Known Member
Licensed 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
 

Smee

Well-Known Member
Licensed 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?
 

Smee

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

pcbtmr

Member
Licensed 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!
 

klaus

Expert
Licensed 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.
 

klaus

Expert
Licensed User
Without more information it's impossible to help you !
Is fileList a List object ?
Are you sure of the content of the DataFolder variable ?
Have you checked the value of fileList.Size ?
etc.

Best regards.
 

mc73

Well-Known Member
Licensed 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)
 

klaus

Expert
Licensed 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.
 

mc73

Well-Known Member
Licensed 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 :)
 

pcbtmr

Member
Licensed 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!
 
Top