dsb has asked for the wisdom of the Perl Monks concerning the following question:
Hello all,
I'm trying to sort on a nested key value. Given a data structure that looks like this:
I want to process them in a sorted order based on the priority value. I'm trying to just turn the hashref into an arrayref, so that it winds up looking like:$data = { Elements => { AlphaKey => { Name => 'Foo', Priority => 2, OtherVal => 'randomthings' }, BetaKey => { Name => 'Bar', Priority => 1, OtherVal => 'morerandomthings' }, }};
It's easy enough to get a sorted list of priorities:$sorted = [ { Name => 'Bar', Priority => 1 OtherVal => 'morerandomthings' }, { Name => 'Foo', Priority => 2, OtherVal => 'randomthings' }, ];
I'm lost on how to build an arrayref-based structure from there. I'm not even sure i'm going down the right path now that I'm typing it out. Thoughts?my @pri = sort { $a <=> $b } map { $data->{Elements}{$_}->{priority} } + keys %{$data->{Elements}};
Back to
Seekers of Perl Wisdom