micmac has asked for the wisdom of the Perl Monks concerning the following question:
Why is broccoli not at the end of the list? I know there's no entry for 9, but why is it inserted there? Thanksmy @food; push @food, { name => "apple", number => 1 }; push @food, { name => "banana", number => 2 }; push @food, { name => "orange", number => 5 }; push @food, { name => "broccoli", number => 9 }; push @food, { name => "grape", number => 10}; my %ranking = ( 1 => 0, 2 => 1, 5 => 2, 10 =>3 ); my @sorted = sort { $ranking{$a->{number}} <=> $ranking{$b->{number}} + } @food; say Dumper(\@sorted); $VAR1 = [ { 'name' => 'apple', 'number' => 1 }, { 'number' => 9, 'name' => 'broccoli' }, { 'number' => 2, 'name' => 'banana' }, { 'name' => 'orange', 'number' => 5 }, { 'number' => 10, 'name' => 'grape' } ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sort an array of hashes by value
by BrowserUk (Patriarch) on Jul 14, 2016 at 00:35 UTC | |
by micmac (Acolyte) on Jul 14, 2016 at 00:48 UTC | |
|
Re: sort an array of hashes by value
by Marshall (Canon) on Jul 14, 2016 at 01:05 UTC | |
by micmac (Acolyte) on Jul 14, 2016 at 20:38 UTC | |
|
Re: sort an array of hashes by value
by AnomalousMonk (Archbishop) on Jul 14, 2016 at 06:20 UTC | |
by micmac (Acolyte) on Jul 14, 2016 at 20:46 UTC |