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

$a is an item from $places and $b is an item from $places, in sort you start with $a and $b not $places
  • Comment on Re: Sorting an array of anonymous hashes with named hashes using hash value

Replies are listed 'Best First'.
Re^2: Sorting an array of anonymous hashes with named hashes using hash value
by Anonymous Monk on May 07, 2016 at 00:07 UTC
    my @sorted_places = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { [ ( values %$_ )[0]->{name}, $_ ] } @$places;

      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