I stumbled across this solution, and found it fell over when it met the likes of the following: qw{ 1.01 1.3 1.02 1.2 };

$A <=> $B does a numerical comparison, but strips any leading zeroes from the number, so how 1.02 and 1.2 are sorted will depend on their original order in the list

No, the spaceship operator  <=> does no stripping of any kind, it does a numerical comparison, and numbers don't have leading zeros , 0002 is 02 is 2

The problem is with the code/regex, which turns decimals into an array of integers , and then does a numerical comparison on each portion -- not gonna work

I'm not too familiar with Tcl's sort order, but what works for me (esp for mp3s) is to lowercase the string, and pad all digits with zeroes (sometimes 6, sometimes 20)

#!/usr/bin/perl -- use strict; use warnings; my @list = ( qw{ bigBoy bigbang bigboy x10y x9y x11y 1.01 1.3 1.02 1.2 x1.1y x1.01y x2.3y x2.1y } ); print join "\n", map { $$_[0] } sort { $$a[1] cmp $$b[1] } map { my $f = $_; $f =~ s/(\d+)/sprintf '%06d',$1/ge; [ $_, lc $f ] } @list; __END__ 1.01 1.02 1.2 1.3 bigbang bigBoy bigboy x1.1y x1.01y x2.1y x2.3y x9y x10y x11y

In reply to Re^3: Dictionary-style sort a la Tcl? by Anonymous Monk
in thread Dictionary-style sort a la Tcl? by argathin

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.