in reply to sorting tree with folders on top

Here is a hack that does what you want:

use File::Basename; sub sort_files { my ($aname,$apath,$asuff)=fileparse $$a{'path'}; my ($bname,$bpath +,$bsuff) = fileparse $$b{'path'}; if ($$a{'file'}) { $aname= 'zzzzzzzz'.$aname; } if ($$b{'file'}) { $bname= 'zzzzzzzz'.$bname; } lc $apath.$aname cmp lc $bpath.$bname; }

I call it hack because it hopes that you never have a dir that is called 'zzzzzzzzzzzzz'. You could use some other character as prefix that is higher than z in the ascii sequence and hoepfully not a valid filename character, but what if that sequence changes or some valid character is even further up in the sequence.

A really solid solution would have to check between files A and B:
1) if A is a directory and in the same directory as B and B is a file then A must come first. Or vice versa
2) If A is in a subdirectory of the directory B is in and B is a file then A must come first. Or vice versa
The second test can be done with a simple regex. Must do something for my day job so I leave that to you ;-)

Replies are listed 'Best First'.
Re^2: sorting tree with folders on top
by diweooy (Novice) on Apr 07, 2009 at 13:56 UTC
    I was trying to follow 1) and 2), but I did not have much success:
    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'}; }

      I got the right result with this code. I left my debug print statements in there so you can test some more. I'm not sure it gets all border cases. Interestingly the same-dir rule turned out to be a special case of the subdir rule (or not? Please test some more. Really)

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

      Note I changed your code so that $adir is simply $$a{'dir'} as it should be the same. Also put 2) before 1) and removed the condition in 2) that b should not be a file

Re^2: sorting tree with folders on top
by diweooy (Novice) on Apr 07, 2009 at 12:16 UTC
    ;-)) ... I like this one :).