good chemistry is complicated,and a little bit messy -LW PerlMonks

### comment on

 Need Help??

You are using a Schwartzian Transform which is good. In this example my expensive function splits the name into three parts (word chars, digits or ., and then everything else). split is a good way to do it as 1) if it fails it will return the whole string (so we can sort on that). If it succeeds then we need to remember that it will split at all the number . sequences so we put the string back together after the first split. If we were to use a straight regex here and it failed to match we would need to have all sorts of fallback cases split() handles it neatly.

The sort then proceeds on first bit, numbers, last bit. If you just wanted to sort foo-bar-0.01.tar.gz then you only need to return @bits[0,1] from func() for it to work correctly.

```sub sort_n {
return
map { \$_->[0] }
sort { \$a->[1] cmp \$b->[1] || \$a->[2] <=> \$b->[2] || \$a->[3] cmp \$b-
+>[3] }
map { [\$_, func(\$_)] } @_;
sub func { my @bits = split /([\d\.]+)/, \$_[0]; return @bits[0,1], j
+oin '', @bits[2..\$#bits] }
}

my @ary = qw( z1z z2a z2z a1a a2b a1.1a a2.1a a0.1a a0.01a b bbb b2b2b
+2b 2b2c a2bc2 );

print "This is what func() does for us\n";
\$" = ', ';
for (@ary) { my @a = func(\$_); print "@a\n" }

print "\nHere is the sorted list\n";
print "\$_\n" for sort_n(@ary);

__DATA__
This is what func() does for
z, 1, z
z, 2, a
z, 2, z
a, 1, a
a, 2, b
a, 1.1, a
a, 2.1, a
a, 0.1, a
a, 0.01, a
b, ,
bbb, ,
b, 2, b2b2b
, 2, b2c
a, 2, bc2

Here is the sorted list
2b2c
a0.01a
a0.1a
a1a
a1.1a
a2b
a2bc2
a2.1a
b
b2b2b2b
bbb
z1z
z2a
z2z

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"\$'\$`\$\"\$\&"&ee&&y&srve&&d&&print

In reply to Re: sort, semi-numeric by tachyon
in thread sort, semi-numeric by superpete

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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2020-12-04 01:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How often do you use taint mode?

Results (58 votes). Check out past polls.

Notices?