How to sort a directory?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By psear

I am trying to load all of the textures from a directory into an array for easy access later using the instructions here

https://docs.godotengine.org/en/stable/classes/class_directory.html#class-directory-method-list-dir-begin

But when I see the order they are being put into the array, it doesn’t make much sense, it looks kind of random. Is there any way to sort the directory alphabetically when it has been opened, so that get_next() iterates through the directory alphabetically, for example?

:bust_in_silhouette: Reply From: jgodfrey

You could certainly do it in a few, discrete steps. Something like:

  • Load an array with all of the file names
  • Sort the array (via Array.sort())
  • Iterate through the sorted array and load the textures

This is actually exactly what I ended up doing. I do think it’s a bit odd that there’s no way to sort directories, though.

psear | 2020-10-20 16:20