#!/usr/bin/perl -l use strict; use warnings; my @aoh =( { 3 => 15, 4 => 8, 5 => 9, }, { 3 => 11, 4 => 25, 5 => 6, }, ... ); for my $h (@aoh) { my @keys_sorted_by_values = sort { $h->{$a} <=> $h->{$b} } keys %$ +h; print "{"; print " $_ => $h->{$_}," for @keys_sorted_by_values; print "},"; } __END__ { 4 => 8, 5 => 9, 3 => 15, }, { 5 => 6, 3 => 11, 4 => 25, }, ...
Note that this doesn't sort the original hashes' entries (in place), i.e. the order as returned by keys, values or each. The latter cannot be achieved this way because hashes have their own intrinsic ordering (appears pseudo-random), which is the result of the particular hashing function being used. In other words, you cannot sort a hash itself, but only what you read from it, e.g. for printing out, or some other processing.
In reply to Re: Array of hash sorting problem
by almut
in thread Array of hash sorting problem
by thillai_selvan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |