Out of interest, I have constructed the following test to compare the performance of various solutions:

use strict; use Benchmark qw(timethese cmpthese); my @array = ( 'four', 'three', 'three|four', 'two', 'two|four', 'two|three', 'two|three|four', 'one', 'one|four', 'one|three', 'one|three|four', 'one|two', 'one|two|four', 'one|two|three', 'one|two|three|four' ); timethese( 10000, {'Schwartzian' => '&Schwartzian()', 'Orcish' => '&Orcish()', 'Direct Sort' => '&DirectSort()', } ); cmpthese( 10000, {'Schwartzian' => '&Schwartzian()', 'Orcish' => '&Orcish()', 'Direct Sort' => '&DirectSort()', } ); sub Schwartzian() { my @sorted_array = map {$_->[1]} sort {$b->[0] <=> $a->[0]} map {[length($_), $_]} @array; } sub Orcish() { my %m; my @sorted_array = sort { ($m{$b} ||= length($b)) <=> ($m{$a} ||= length($a)) } @array; } sub DirectSort() { my @sorted_array = sort {length $b <=> length $a} @array; }
And the result is as follows on a 1.8GHz Pentium 4:

Benchmark: timing 100000 iterations of Direct Sort, Orcish, Schwartzia +n... Direct Sort: 4 wallclock secs ( 4.00 usr + 0.02 sys = 4.02 CPU) @ 2 +4900.40/s Orcish: 10 wallclock secs ( 9.47 usr + 0.00 sys = 9.47 CPU) @ 10 +560.78/s Schwartzian: 13 wallclock secs (13.77 usr + 0.00 sys = 13.77 CPU) @ 7 +264.80/s Rate Schwartzian Orcish Direct Sort Schwartzian 7272/s -- -31% -71% Orcish 10561/s 45% -- -58% Direct Sort 24907/s 242% 136% --
It turns out that the Schwartzian sort is the slowest of all. (The Orcish Maneuver was put in out of curiosity to see how expensive is the length function.)

From the result I have deduced a couple of interesting results:

1) Orcish Maneuver is useless unless on expensive operations, where the operation takes significantly longer time than the hash table insert and lookup.

2) Schwartzian Transformation on a simple array is an expensive operation.

3) Simplest sort is the best for very fast functions in the comparison, such as length.


In reply to Re: Re: Sort Array by length of Value by Roger
in thread Sort Array by length of Value by Emanuel

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.