For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2007 > Sorting the items in a directory









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sorting the items in a directory
Nigel Peck

2007-04-27, 6:58 pm


Hi,

I have a list containing the names of all items in a directory. I want
to sort it by non-directories first and then directories, with a
secondary sort in alphabetical order.

I currently have:

--------------------------------
my @items = sort {

my $a_path = $args->{directory_from} . '\\' . $a;
my $b_path = $args->{directory_from} . '\\' . $b;

( -d $a_path ) ? 1 : 0;

} readdir $directory;
--------------------------------

which gets me the non-directories first and directories after but not in
alphabetical order.

I don't want to rely on the alphabetical ordering from the readdir as I
believe that Perl's sort does not guarantee to maintain ordering in
future releases?

Can't get my head round this, please help! :)

Cheers,
Nigel
Jeff Pang

2007-04-27, 6:58 pm

I'm sorry that just be clear you want the non-directory first,then
simply change the codes to:

my @items = map { $_->[0] }
sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] }
map { -d $_ ? [$_,1] : [$_,0] } readdir DIR;

2007/4/27, Jeff Pang <pangj@earthlink.net>:
> 2007/4/27, Nigel Peck <nigel@miswebdesign.com>:
>
> Hello,
>
> I've tested, this could work for you.
>
> my @items = map { $_->[0] }
> sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] }
> map { -d $_ ? [$_,0] : [$_,1] } readdir DIR;
>
> --
> Chinese Practical Mod_perl book online
> http://home.arcor.de/jeffpang/mod_perl/
>



--
Chinese Practical Mod_perl book online
http://home.arcor.de/jeffpang/mod_perl/
Jeff Pang

2007-04-27, 6:58 pm

2007/4/27, Nigel Peck <nigel@miswebdesign.com>:
>
> Hi,
>
> I have a list containing the names of all items in a directory. I want
> to sort it by non-directories first and then directories, with a
> secondary sort in alphabetical order.


Hello,

I've tested, this could work for you.

my @items = map { $_->[0] }
sort { $a->[1] <=> $b->[1] or $a->[0] cmp $b->[0] }
map { -d $_ ? [$_,0] : [$_,1] } readdir DIR;

--
Chinese Practical Mod_perl book online
http://home.arcor.de/jeffpang/mod_perl/
Rob Dixon

2007-04-27, 6:58 pm

Nigel Peck wrote:
>
> Hi,
>
> I have a list containing the names of all items in a directory. I want
> to sort it by non-directories first and then directories, with a
> secondary sort in alphabetical order.
>
> I currently have:
>
> --------------------------------
> my @items = sort {
>
> my $a_path = $args->{directory_from} . '\' . $a;
> my $b_path = $args->{directory_from} . '\' . $b;
>
> ( -d $a_path ) ? 1 : 0;
>
> } readdir $directory;
> --------------------------------
>
> which gets me the non-directories first and directories after but not in
> alphabetical order.
>
> I don't want to rely on the alphabetical ordering from the readdir as I
> believe that Perl's sort does not guarantee to maintain ordering in
> future releases?
>
> Can't get my head round this, please help! :)
>
> Cheers,
> Nigel
>


my @items = sort {

my $a_path = $args->{directory_from} . '\' . $a;
my $b_path = $args->{directory_from} . '\' . $b;

-d $b_path cmp -d $a_path
or
$a cmp $b;

} readdir $directory;



HTH,

Rob
John W. Krahn

2007-04-27, 6:58 pm

Nigel Peck wrote:
>
> Hi,


Hello,

> I have a list containing the names of all items in a directory. I want
> to sort it by non-directories first and then directories, with a
> secondary sort in alphabetical order.
>
> I currently have:
>
> --------------------------------
> my @items = sort {
>
> my $a_path = $args->{directory_from} . '\' . $a;
> my $b_path = $args->{directory_from} . '\' . $b;
>
> ( -d $a_path ) ? 1 : 0;
>
> } readdir $directory;
> --------------------------------
>
> which gets me the non-directories first and directories after but not in
> alphabetical order.
>
> I don't want to rely on the alphabetical ordering from the readdir as I
> believe that Perl's sort does not guarantee to maintain ordering in
> future releases?
>
> Can't get my head round this, please help! :)



my @items = map "$args->{directory_from}/$_", do {
my ( @dirs, @files );
push @{ -d "$args->{directory_from}/$_" ? \@dirs : \@files }, $_
for sort readdir $directory;
@files, @dirs;
};




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Nigel Peck

2007-04-27, 6:58 pm


Thanks Jeff, thanks Rob.

I used your solution Jeff and it's working a treat.

Cheers,
Nigel

Rob Dixon wrote:
> Nigel Peck wrote:
>
> my @items = sort {
>
> my $a_path = $args->{directory_from} . '\' . $a;
> my $b_path = $args->{directory_from} . '\' . $b;
>
> -d $b_path cmp -d $a_path
> or
> $a cmp $b;
>
> } readdir $directory;
>
>
>
> HTH,
>
> Rob
>

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com