I am filling in an array with file names from a folder. No problem there. But when I finish, I want to sort them, but can’t get the program to run. It keeps asking for a “delegate” and after going through the documentation, I’m not sure what I am supposed to do.
Here is my code
For i As Integer = 0 To thePictureFolder.Count - 1
Var filename As FolderItem = thePictureFolder.ChildAt(i)
If filename <> Nil And filename.Exists Then
// make the name lowercase
Var lowerName As String = filename.Name.Lowercase
For Each ext As String In extList
If lowerName.EndsWith(ext) Then
validFiles.Append(filename)
// for testing only
System.DebugLog "The file name is " + filename.Name
Exit // stop checking extensions for this file
End If
Next
End If
next
validfiles.sort
The validFiles array is an array of FolderItems rather than strings. Xojo doesn’t know how to sort an array of FolderItems so it expects you to provide a method (AKA a “delegate”) that knows how to sort FolderItems.
Change the validFiles array to a string array, and store the filenames in it. Then you can sort without providing your own sorting method.
What @Wayne_Golding is saying is that you could add a Function like
Public Function PathCompare(value1 As FolderItem, value2 As FolderItem) As Integer
If value1.NativePath > value2.NativePath Then Return 1
If value1.NativePath < value2.NativePath Then Return -1
Return 0
End Function
Or just make a second array called names() as String, add lowername to it every time you add something to validfiles and then at the end, use ‘names.sortWith(validfiles)’.
Note - I think your variable names are part of the confusion. The variable named “Filename” should probably just be “File” because it holds a whole folderitem in this case.
I’m also going to make the suggestion of using an iterator for traversing this folder because they are so much faster for this. You can also eliminate a loop.
Var names() as string
For each File as FolderItem in thePictureFolder.Children
Var lowerName As String =
file.Name.Lowercase
If extList.indexOf(file.extension) > -1 then
validFiles.Append(file)
Names.Add(lowername)
// for testing only
System.DebugLog "The file name is " + filename.Name
End If
Next
Names.sortwith(validfiles)
I f.e. used to make the mistake of trying to keep variable names as short as possible. I later regretted that. Today, for example, I tend to use the first three characters to indicate the data type, and then I like to use the variable’s purpose as the name in camel-case format.
Var intCountOfFilesInFolder As Integer
Var dicCustomerAndType As Dictionary
Like when I build my interface and name my buttons, etc like
buttonOK
buttonClose
or
labelForTextfield, etc
And one question, and please don’t judge, when in the thread people talk about creating a Function, are they talking about a Method, or that XojoScript stuff.
I’ve noticed that this usually refers to a method. In my post, I actually meant a method. In other languages, this is often called a function or something else.
AFAIK, in traditional CS terminology, functions and methods are both subroutines. A function is a subroutine that returns a value and a method is a subroutine that doesn’t.
In Xojo parlance they’re both Methods, but if you copy a method from the IDE navigator and paste it into a text editor, it will start with “Function” if it returns a value and “Sub” if not.
But yes, people tend to use the terms interchangeably in casual conversation.