well,
Sort::Key::Natural is a general solution that doesn't make any assumption about the strings passed. For instance it doesn't have a limit on the number of groups or on the size of the numbers that can be embedded in the strings.
But if you know the data is of the format /^K-2-D-\d+\w$/ and want to create a sorter taking advantage of it, a Sort::Key based solution is still the fastest:
use Sort::Key::Multi 'ii_keysort';
...
cmpthese(-10, {
...
SKMii => sub {
my @s = ii_keysort { /(\d+)([A-Z])/; $1, ord $2 }
+@data
} } );
gives me:
SN_nsort 46.9/s -- -81% -89% -90% -92%
+ -96% -96%
SKN_natsort 244/s 420% -- -43% -47% -58%
+ -79% -81%
ST_sub 427/s 812% 75% -- -7% -27%
+ -62% -66%
GRT_sub 460/s 881% 89% 8% -- -22%
+ -60% -64%
GRT_sprintf 587/s 1152% 141% 37% 28% --
+ -48% -54%
GRT_pack 1136/s 2323% 366% 166% 147% 94%
+ -- -11%
SKMii 1272/s 2614% 422% 198% 177% 117%
+ 12% --
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.