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 | |
by jethro (Monsignor) on Apr 07, 2009 at 15:31 UTC | |
|
Re^2: sorting tree with folders on top
by diweooy (Novice) on Apr 07, 2009 at 12:16 UTC |