Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Below is the script I am using for testing other sort routines:
use List::Util qw(min); my @path = qw( /abc /abc/1/2/a/b/c /abc/2 /abc/def/1/2/a/b/c /abc!def/1/2/a/b/c /abc-def/1/2/a/b /abcdef/1/2/a/b/c /usr/a ); say 'Wanted path order:'; say ' 'x4, $_ for @path; my @sorted = sort @path; say 'Lexical order (default sort):'; say ' 'x4, $_ for @sorted; say 'Ordered by dirname, then basename:'; @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] or $a->[2] cmp $b->[2] } map { [$_, m{^(.*/)?(.*)}s ] } @path; say ' 'x4, $_ for @sorted; say 'Ordered by path components:'; @sorted = sort bypath @path; say ' 'x4, $_ for @sorted; say 'Ordered by path components (with caching):'; @sorted = map { $_->[0] } sort { for my $i (0 .. min $#{$a->[1]}, $#{$b->[1]}) { return $a->[1][$i] cmp $b->[1][$i] if $a->[1][$i] ne $b->[ +1][$i]; } return $#{$a->[1]} <=> $#{$b->[1]}; } map { [$_, [split '/'] ] } @path; say ' 'x4, $_ for @sorted; exit; # https://stackoverflow.com/questions/5124994/sorting-directory-filena +mes-with-perl/19506331#19506331 sub bypath { my @a = split m'/', $a; my @b = split m'/', $b; for (my $i = 0; $i <= $#a; $i++) { last if $i > $#b; return $a[$i] cmp $b[$i] if $a[$i] cmp $b[$i]; } return $#a <=> $#b; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sorting path names with Sort::Key
by salva (Canon) on Jan 10, 2025 at 09:05 UTC | |
by ikegami (Patriarch) on Jan 10, 2025 at 12:42 UTC | |
by Anonymous Monk on Jan 10, 2025 at 13:40 UTC | |
by salva (Canon) on Jan 23, 2025 at 13:28 UTC | |
by Anonymous Monk on Jan 10, 2025 at 18:22 UTC | |
by Anonymous Monk on Jan 10, 2025 at 09:26 UTC | |
|
Re: Sorting path names with Sort::Key
by harangzsolt33 (Deacon) on Jan 10, 2025 at 09:36 UTC |