You probably want to do
$referrers[$cnt] = [ $element, $name, $count ]; $cnt++;
instead. Actually, scratch that, you want push @referrers, [ $element, $name, $count ];

Avoid index variables in Perl whenever possible (which is very nearly always due to push, pop, shift, unshift, splice and foreach) - they are a source of "one off" mistakes. Plus, the array operators are more efficient.

Anyway, using anonymous arrays, sorting becomes trivial: my @sorted_referrers = sort { $a->[2] cmp $b->[2] } @referrers; For the record, here's a simple version that will do this using your code and split: my @sorted_referrers = sort { (split("|", $a))[2] cmp (split("|",$b))[2] } @referrers; The following is a much more efficient way to do it:
my @sorted_referrers = map { $_->[1] } sort { $a->[0] cmp $b->[0] }, map { [ (split("|"))[2], $_ ] } @referrers;
But honestly, it is nonsense to jump through all these hoops to work with stringified data when you have it available in native form to begin with.

Makeshifts last the longest.


In reply to Re: Sorting CSV array by Aristotle
in thread [untitled node, ID 178427] by Samn

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.