Sorting and delegate?

Hi all.

I am confused on something.

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

Can someone explain what i am to do?

Regards

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.

2 Likes

You need to write a delegate method to sort your array of objects. Check out Array Sort & scroll down to “Sort(AddressOf methodName)”.

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

and then do a

validfiles.Sort(AddressOf PathCompare)

Correct, @Wayne_Golding ? :slight_smile:

1 Like

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)
4 Likes

see comparer method (wayne and sascha)

and please use better variable names.

Hi Markus

May I ask what is wrong with my variable names?

Just curious if I am doing something wrong?

Regards

Thanks for the information everyone; I will give your suggestions a try and get back to you.

Regards

Just, what @Greg_O said :slight_smile:

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

Some tips and recommendations: How to Write Better Names for Your Variables, Functions, and Classes – With Examples

4 Likes

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.

Regards

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.

Function = Method

2 Likes

the code conventions i try to follow

good naming help yourself better reading and understanding and others if you share this source code.

1 Like

This is the naming convention I mostly* stick to:
Naming Conventions | BKeeney Briefs - Internet Archive

I give credit to this naming scheme for making it super simple to update code for API 2.0 with RegEx.

1 Like

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.

1 Like