in reply to sorting tree with folders on top

sub sort_files {
        # if both in the same folder and one is folder, put it top
        if ($$a{'dir'} eq $$b{'dir'}) {

this if only compares whether the hash claims that they are in what you call "dir" - here it is always "/c", hence you will put all folders on top and all files below.

What you need - and that's why I asked beforehand - is some way of finding out that files are either in your "main"/parent directory (then sort them downwards) or if they are in a subdir (then list them directly below their subdirectory).
Regards,
svenXY

Replies are listed 'Best First'.
Re^2: sorting tree with folders on top
by diweooy (Novice) on Apr 07, 2009 at 10:52 UTC
    my mistake. it should be:
    my @arr = ( { 'path' => '/c/', dir => '/' }, { 'path' => '/c/a1.mp3', 'file' => 1, 'dir' => '/c' }, { 'path' => '/c/a2.mp3', 'file' => 1, 'dir' => '/c' }, { 'path' => '/c/bb/aa1.mp3', 'file' => 1, 'dir' => '/c/bb' }, # th +is is /c/bb { 'path' => '/c/bb/aa2.mp3', 'file' => 1, 'dir' => '/c/bb' }, # th +is is /c/bb { 'path' => '/c/bb/', 'dir' => '/c' }, { 'path' => '/c/cc/', 'dir' => '/c' }, { 'path' => '/c/aa/', 'dir' => '/c' } );
    key "dir" is only here to make it faster to compare where which file/subfolder is located. dir is nothing else but everything from the path until last slash. path =~ s/^(.*)(\/.*)$/$1/;
Re^2: sorting tree with folders on top
by Anonymous Monk on Apr 07, 2009 at 10:44 UTC
    Add this
    #fix dir for my $i (@arr){ $i->{dir} = $1 if $i->{path} =~ m{^(.+)/}; }
    one step closer :)
    /c/ /c/a1.mp3 /c/a2.mp3 /c/aa/ /c/bb/ /c/bb/aa1.mp3 /c/bb/aa2.mp3 /c/cc/