Is there any way to guarantee an order from the list returned by readdir
?
I have the code:
opendir(my $DIR, $src) or die "Error opening $src";
# Loop for each file in the directory
while (my $file = readdir($DIR))
{
print "$file\n";
}
But it returns in random order. Now I know there are plenty of solutions via a quick Google search, but I can't find the exact order I need. Basically I want the folders to appear FIRST or LAST, and not in between the files.
For example, right now if I have the folder structure:
folder
folder
file1
file2
file3
I get the result:
file2
folder
folder
file1
file3
When really I want:
folder
folder
file1
file2
file3
Or:
file1
file2
file3
folder
folder
Any way to achieve this?