sub sort_files { my $adir = $1 if $$a{path} =~ m/^(.*)\//; my $bdir = $1 if $$b{path} =~ m/^(.*)\//; # a and b in the same dir if ($adir eq $bdir) { if ($$a{'file'} && !$$b{'file'}) { return -1; } elsif (!$$a{'file'} && $$b{'file'}) { return 1; } return lc $$a{'path'} cmp lc $$b{'path'}; } # b in subdir of a if ($bdir =~/^$adir\// && $$a{'file'} && !$$b{'file'}) { return -1; # a in subdir of b } elsif ($adir =~/^$bdir\// && $$b{'file'} && !$$a{'file'}) { return 1; } lc $$a{'path'} cmp lc $$b{'path'}; }