How can I sort a set of strings by the season they represent? My comparison function apparently works, but when I actually use it, it only returns the input array:
use warnings; use strict; # The terms are to be sorted in this order: # SM (summer) # AU (autumn) # WI (winter) # SP (spring) sub by_term { my($t1, $t2)=@_; if ($t1 eq 'SM') { return $t2 eq 'SM' ? 0 : -1 } if ($t1 eq 'AU') { return 1 if $t2 eq 'SM'; return $t2 eq 'AU' ? 0 : -1 } if ($t1 eq 'WI') { return 1 if $t2 =~ /SM|AU/; return $t2 eq 'WI' ? 0 : -1 } if ($t1 eq 'SP') { return $t2 eq 'SP' ? 0 : 1 } } my @a = @b = qw(SM AU WI SP); # by_term() returns the right integer for each possible comparison for my $a (@a) { for my $b (@b) { warn "$a - $b" . by_term($a, $b); } } # however, the test suite yields only the input array! my @try = ( [qw(WI AU SM SP)], [qw(SP AU SM WI)], [qw(AU WI SP SM)] ); for my $try (@try) { my @sort = sort by_term @$try; warn "@sort"; }

script output

[tbrannon@Ghostdc7600] [/cygdrive/l/Temp-Erased_On_Monthend] perl by_t +erm.pl SM - SM0 at by_term.pl line 23. SM - AU-1 at by_term.pl line 23. SM - WI-1 at by_term.pl line 23. SM - SP-1 at by_term.pl line 23. AU - SM1 at by_term.pl line 23. AU - AU0 at by_term.pl line 23. AU - WI-1 at by_term.pl line 23. AU - SP-1 at by_term.pl line 23. WI - SM1 at by_term.pl line 23. WI - AU1 at by_term.pl line 23. WI - WI0 at by_term.pl line 23. WI - SP-1 at by_term.pl line 23. SP - SM1 at by_term.pl line 23. SP - AU1 at by_term.pl line 23. SP - WI1 at by_term.pl line 23. SP - SP0 at by_term.pl line 23. WI AU SM SP at by_term.pl line 35. SP AU SM WI at by_term.pl line 35. AU WI SP SM at by_term.pl line 35.

update:Khisanth figured it out... sort() calls the sorting routine with $a, $b not in the normal fashion...

<Khisanth> yeah, replacing my ($t1, $t2) = @_; with my ($t1, $t2) = ($a,$b);
	   gives SM AU WI SP


In reply to Sorting by season by metaperl

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.