in reply to How to sort an array of hashes based on one of the values in the hash?

############################################## # Wrong! (thanks tobylink) #@AoH = sort { $a->{Son} <=> $b->{Son} } @AoH; # Correct. @AoH = sort { $a->{Son} cmp $b->{Son} } @AoH;


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
  • Comment on Re: How to sort an array of hashes based on one of the values in the hash?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to sort an array of hashes based on one of the values in the hash?
by tobyink (Canon) on Mar 08, 2013 at 15:49 UTC

    Yes, but you want cmp not <=>.

    cmp is for ASCIIbetical sorting; <=> is for numeric sorting. (And Unicode::Collate is for proper, decent alphabetical sorting.)

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
          Yes, but you want cmp not <=>.

      Dammit! My fingertips did it again! I thought one thing and typed another!


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg