Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
$string = "(x1,x1)(x2,y2)........(xn,yn)"

A real, sortable string together with its expected sorted output form would have been helpful, both to give the monks something to work with and to clarify the problem. (The apparently erroneous  (x1,x1) first term also doesn't help.)

... sorted by the x co-ordinate, then by y. ... reorder the string by y co-ordinate first, then by x ...

I'm confused by this as to whether you want to sort "by x then by y", or "by y then by x". I will assume the former.

... sorted by the x co-ordinate ...

This suggests to me a Update: ascending numeric sort.

Given these assumptions, here's an example of the sort of thing explained in Corion's link.

>perl -wMstrict -le "my $u = '(x1,y9)(x11,y8)(x1,y7)(x11,y6)(x2,y5)(x22,y4)(x2,y3)(x22,y2)'; ;; my $s = join '', map { $_->[0] } sort { $a->[1][0] <=> $b->[1][0] || $a->[1][1] <=> $b->[1][1] } map { [ $_, [ $_ =~ m{\d+}xmsg ]] } split m{ (?<= \)) (?= \() }xms, $u ; ;; print qq{'$s'}; " '(x1,y7)(x1,y9)(x2,y3)(x2,y5)(x11,y6)(x11,y8)(x22,y2)(x22,y4)'

Note: The original string must be split into an array because sort only works on arrays.

Update: Here's a version that will be significantly faster if you're sorting lotsa strings or very long strings. See A Fresh Look at Efficient Perl Sorting for a discussion.

>perl -wMstrict -le "my $u = '(x1,y9)(x11,y8)(x1,y7)(x11,y6)(x2,y5)(x22,y4)(x2,y3)(x22,y2)'; ;; use constant WIDTH => 10; my $s = join '', map { substr $_, WIDTH+WIDTH } sort map { sprintf '%0*4$d%0*4$d%s', m{\d+}xmsg, $_, WIDTH } split m{ (?<= \)) (?= \() }xms, $u ; ;; print qq{'$s'}; " '(x1,y7)(x1,y9)(x2,y3)(x2,y5)(x11,y6)(x11,y8)(x22,y2)(x22,y4)'

In reply to Re: (Hopefully) simple sorting question by AnomalousMonk
in thread (Hopefully) simple sorting question by qwerty155

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 How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2022-11-28 02:26 GMT
Find Nodes?
    Voting Booth?