I'm not really interested in Perl Golf, hence this late reply, but I'd like to contribute by stating my favourite approach for natural sorting, that apparently nobody has touched in this thread. The basis is this:
split /(\d+)/
This will split the example strings like this:
1
"", 1
A
"A"
amstelveen
"amstelveen"
Amsterdam
"Amsterdam"
Amsterdam5
"Amsterdam", 5
Amsterdam40
"Amsterdam", 40
Amsterdam40b
"Amsterdam", 40, "b"
Amsterdamned
"Amsterdamned"
and now for some more complex examples:
Chapter 1 Section 3
"Chapter ", 1, "Section ", 3
Chapter 1 Section 10
"Chapter ", 1, "Section ", 10

Now the basic trick to do natural sorting, is partwise comparison of the items, comparing the even numbered items (starting at 0) with alphabetical comparison, case insensitive even if you like, and the odd numbered items with numerical comparison.

This is code that does basically that:

sub natcomp { my @a = split /(\d+)/, $a; my @b = split /(\d+)/, $b; my $last = min(scalar @a, scalar @b)-1; my $cmp; for my $i (0 .. $last) { unless($i & 1) { # even $cmp = lc $a[$i] cmp lc $b[$i] || $a[$i] cmp $b[$i] and re +turn $cmp; }else { # odd $cmp = $a[$i] <=> $b[$i] and return $cmp; } } return scalar @a <=> scalar @b; # shortest array comes first }

Let's try this with:

chomp(my @array = <DATA>); $\ = "\n"; print for sort natcomp @array; __DATA__ 1 A amstelveen Amsterdam Amsterdam40 Amsterdam40b Amsterdam5 Amsterdamned Chapter 1 Section 10 Chapter 1 Section 3 Chapter 10 Section 2 Chapter 2 Section 1
(Note that the DATA section is sorted alphabetically, and that it contains an empty string)

The result is:


1
A
amstelveen
Amsterdam
Amsterdam5
Amsterdam40
Amsterdam40b
Amsterdamned
Chapter 1 Section 3
Chapter 1 Section 10
Chapter 2 Section 1
Chapter 10 Section 2

I hope that order is to your liking.


In reply to Re: A new golf challenge - Natural Sorting by bart
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.