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 { # if both in the same folder and one is folder, put it top if ($$a{'dir'} eq $$b{'dir'}) { if ($$a{'file'} && !$$b{'file'}) { return 1; } elsif (!$$a{'file'} && $$b{'file'}) { return -1; } } # compare lc $$a{'path'} cmp lc $$b{'path'}; } #### /c/ /c/aa/ /c/bb/ /c/cc/ /c/a1.mp3 <<<< file! it should at the end of the list /c/a2.mp3 <<<< file! should at the end of the list /c/bb/aa1.mp3 <<<< this should be below /c/bb/ /c/bb/aa2.mp3 <<<< this should be below /c/bb/