Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Your sort operator needs to impose a lexicographic ordering on the lists defined by your "paths", as in this example:
#!/usr/bin/perl use strict; use warnings; # parse the paths into lists my @data; while ( <DATA> ) { chomp; push @data, [ split m:/:, $_ ]; } sub compare_lists_lexicographically { # make copies of the input lists my @left = @$a; my @right = @$b; # chew threw the lists until # a) we discover they're different lengths, or # b) the elements are different, or # c) neither while ( 1 ) { if ( !@left && !@right ) { # ran out at the same time: they're equal return 0; } elsif ( !@left ) { # left is shorter than right: left is first return -1; } elsif ( !@right ) { # right is shorter than left: right is first return 1; } else { # check the next element from each list my ( $l, $r ) = ( shift @left, shift @right ); # NB: use < if fields are numeric, lt otherwise if ( $l < $r ) { # next element of left comes first return -1; } elsif ( $r < $l ) { # next element of right comes first return 1; } # else equal, keep checking } } } # display the output printf "%s\n", join '/', @$_ for sort compare_lists_lexicographically +@data; __DATA__ 0/1/2/3 0 0/4/5/6 0/4 0/1 0/1/2 0/4/5 0/10/111/145 0/10/111 0/10
HTH,
--athomason

In reply to Re: Sorting path like strings by athomason
in thread Sorting path like strings by BUU

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2021-12-05 05:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (31 votes). Check out past polls.

    Notices?