What if we would encode the path depth as a character and place it in front of each path and also convert every '/' character to "\xFF" temporarily while sorting? What do you think?

#!/usr/bin/perl use strict; use warnings; my @path = qw( /abc/def/1/2/3/4/5/6/7/8 /abc /abc/1/2/a/b/c /abc/2 /tmp/1/2/a/../b/c /abc/def/1/2/a/b/c abc/def/../../john/Desktop/work abc/../john/Desktop/work ../john/Desktop/work /abc!def/1/2/a/b/c/. /abc/def /abc-def/1/2/a/b /./home ./abcdef/1/2/a/b/c /tmp /usr/a ); foreach (@path) { # Let's get rid of . and .. $_ =~ s!^\./!!; $_ =~ s!^/\./!/!; $_ =~ s!/\.$!/!; $_ =~ s!/\./$!/!; $_ =~ s!/\./!/!g; while ($_ =~ s![^/.]+/[.]{2}/!!) {} # Next, we change every "/" to "\xFF" # and insert the path depth encoded as a # character in front of the path. my $test = substr($_, 1); my $depth = $test =~ tr|/|/|; $_ =~ tr|/|\xff|; $_ = chr($depth) . $_; } @path = sort @path; # Sort list foreach (@path) { $_ = substr($_, 1); $_ =~ tr|\xff|/|; # Undo changes print "\n $_"; # Print sorted list }

In reply to Re: Sorting path names with Sort::Key by harangzsolt33
in thread 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.