in reply to Re: Sorting an array of anonymous hashes with named hashes using hash value
in thread Sorting an array of anonymous hashes with named hashes using hash value

my @sorted_places = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [ ( values %$_ )[0]->{name}, $_ ] } @$places;
  • Comment on Re^2: Sorting an array of anonymous hashes with named hashes using hash value
  • Download Code

Replies are listed 'Best First'.
Re^3: Sorting an array of anonymous hashes with named hashes using hash value
by nysus (Parson) on May 07, 2016 at 12:06 UTC

    Much trickier than I thought. Thanks!

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      Hi nysus,

      There is one caveat to the AM's code: because hash ordering is random, ( values %$_ )[0] picks a random value from that hash. So if I change your example data structure like so: $places->[0]{123}{name} = "Alpha";, the sort will give random results. So if you use that sort method, make sure that you know each hash at that level only has one key.

      Hope this helps,
      -- Hauke D