Maybe you would like this solution:
use strict; use warnings; 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' }, { 'path' => '/c/bb/aa2.mp3', 'file' => 1, 'dir' => '/c/bb' }, { 'path' => '/c/bb/', 'dir' => '/c' }, { 'path' => '/c/cc/', 'dir' => '/c' }, { 'path' => '/c/aa/', 'dir' => '/c' } ); @arr = sort sort_files @arr; foreach (@arr) { my %h = %{$_}; print $h{'path'}."\n"; } # putting the folders on the top works, but... see output sub sort_files { my $apath = $$a{path}; my $bpath = $$b{path}; $apath =~ s/([^\/]*\/)/\x00$1/g; $bpath =~ s/([^\/]*\/)/\x00$1/g; $apath cmp $bpath; }
If performance is an issue, you could pre-process your array to include the modified path strings so that you only have to calculate them once.
In reply to Re: sorting tree with folders on top
by ig
in thread sorting tree with folders on top
by diweooy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |