Search code examples
perlsortingdirectoryreaddir

Perl readdir in order


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?


Solution

  • You can sort by putting folders first and then sorting by file/dir name,

    # $src pointing to folder open with opendir
    my @sorted_dir = 
      map $_->[0],
      sort {
        $a->[1] <=> $b->[1]
          ||
        $a->[0] cmp $b->[0]
      }
      map [ $_, -f "$src/$_" ],
      readdir($DIR);
    

    While similar effect can be achieved with,

    for my $file (sort { -f "$src/$a" <=> -f "$src/$b" } readdir($DIR)) {
      print "$file\n";
    }
    

    it's slower and inefficient as it more often goes to file system checking if directory entry is a plain file.