I need to sort a bunch of path names and wanted to use Sort::Key because I'm already using it several other places in my code. A proper path sorting algorithm requires splitting the path into its components for comparison, and the number of components can be arbitrary. All of the documentation for Sort::Key and its family of other modules assume the number of comparison keys is known. Is it possible to use Sort::Key for this use case and if so, how? Thanks.

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; }

In reply to Sorting path names with Sort::Key by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.