Sort FolderItem children

Hi all,
I’m trying to list files from a directory. That’s pretty simple.
But, the files collection isn’t sorted (depending on the OS).
I read a post here talking about sorting and the solution should be using SortWith.
But I can’t figure out how to implement this with SortWith.
Any advice ?
Thanks in advance.

Load the directory contents file names in an array, then sort the array.

And if you want to do something with that array, do it sequencially from (0 or 1) the array start.

LR entry for Array:
http://documentation.xojo.com/api/language/array.html

I did that already.
My need is to populate a directory including files and sub-directories etc…
So I have to sort the FolderItems collection itemself by file names.
A single array won’t make it.

I hope you want to do that on macOS only (since Windows there is very slow).

a. This is not the end (tu veux faire quoi ?)

b. Recursive search is handy for that.

c. Depending on a, you have solutions (storing in a dictionary for example).

I am a loss of idea because I cannot think at a use.

Now, I created a project with a Hierarchical Listbox that allows me to display a complete folder hierarchy (what you wrote).

I made a blog article with example code to copy:

https://mbs-plugins.de/archive/2019-06-15/Tip_of_the_day_Folderitem_item/monkeybreadsoftware_blog_xojo

[quote]So I have to sort the FolderItems collection itemself by file names.
A single array won’t make it.[/quote]

Why not?

[code]dim cnt as integer =parentfolder.count
dim diritems() as string
redim diritems(cnt)
for i as integer =1 to cnt
diritems(i) = parentfolder.item(i).Name
next
diritems.Sort

//now do something with the list.
//to get the file later it is parentfolder.child(diritems(someindex))[/code]

Thanks @Christian Schmitz :slight_smile:
That’s exactly what I was looking for : a way to sort the folderitems collection using SortWith.

Valry - I am working on a program that does exactly what you describe. I have a method that recursively walks through the directory structure and returns the entire directory tree into a single flat file in an array of a class with properties Path and FileName.

After loading the directory into the array, the routine sorts the directory array using the following code:

SourceDirectory.Sort (AddressOf CompareDirectoryByPathName)

And the CompareDirectoryByPathName code is:

IF p1.Path > p2.Path THEN RETURN 1 ' Compare path names IF p1.Path < p2.Path THEN RETURN -1 IF p1.FileName > p2.FileName THEN RETURN 1 ' Path names are the same - compare file names IF p1.FileName < p2.FileName THEN RETURN -1 RETURN 0 ' Path and file names are identical

You could just as easily have an array of strings - my code requires the path and filenames to be kept separate.

I have used this method successfully to sort a directory array of over 1 million entries.

Alex