note AnomalousMonk <blockquote><i><c> \$string = "(x1,x1)(x2,y2)........(xn,yn)" </C></I></BLOCKQUOTE> <p> 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 <c> (x1,x1) </C> first term also doesn't help.) </p> <blockquote><i> ... sorted by the x co-ordinate, then by y. ... reorder the string by y co-ordinate first, then by x ... </I></BLOCKQUOTE> <p> 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. </P> <blockquote><i> ... sorted by the x <b>co-ordinate</B> ... </I></BLOCKQUOTE> <p> This suggests to me a <small><b>Update:</B> ascending</SMALL> numeric sort. </P> <p> Given these assumptions, here's an example of the sort of thing explained in [Corion]'s link. </P> <c> >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 { \$_-> } sort { \$a-> <=> \$b-> || \$a-> <=> \$b-> } 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)' </C> <p> <b>Note:</B> The original string <i>must</I> be [doc://split] into an array because [doc://sort] only works on arrays. </P> <p> <b>Update:</B> Here's a version that will be significantly faster if you're sorting lotsa strings or very long strings. See <a href="http://www.sysarch.com/Perl/sort_paper.html">A Fresh Look at Efficient Perl Sorting</a> for a discussion. </P> <c> >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)' </C> 907929 907929