It is small (97 strokes, including the meaningful newline), but it is also very fast. There are certainly places where a few characters can be shaved but several of those break subtle features such as one demonstrated in the example code: sorting the sample data correctly even when each record ends with a newline.

use strict; use warnings; sub naturalSort { my$i;s/(\d+)/pack"aNa*",0,length$1,$1/ge,$_.=$".$i++for # 345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345 my@x=map{lc}@_;@_[map{(split)[-1]}sort@x] #8 6 2345678 7 2345678 8 2345678 9 234567 } my @data= <DATA>; chomp @data if $\; print for naturalSort(@data); __END__ amsTerdam40 amstelveen 1 AmstErdam5 Amsterdam amsterDamed A

Run it normally or with "perl -l ..." to see that it sorts the same with or without the newlines in the strings being sorted.

- tye        


In reply to Re: A new golf challenge - Natural Sorting (OneTruePerlSort) by tye
in thread A new golf challenge - Natural Sorting by theroninwins

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.