You cannot sort a hash due to hash is unordered data structure. Every hash value associated with key but not

with position. Although you can _output_ hash elements in sorted order by converting hash to list of hash keys,

and sorting this list:

my %a;

$a{'x1'}{'d'}=5;

$a{'x1'}{'a'}=2;

$a{'x1'}{'g'}=1;

$a{'x2'}{'x'}=5;

$a{'x2'}{'g'}=2;

$a{'x3'}{'r'}=13;

$a{'x3'}{'c'}=3;

$a{'x3'}{'b'}=4;

$a{'x3'}{'y'}=18;

# $okey iterates thru all 'outer' hash keys ordered

# by their names

foreach my $okey (sort keys %a) {

# $inner is a reference to anonymous hash

# (2nd level of your data structure)

my $inner = $a{$okey};

# @sorted contains 'inner' keys ordered by values

my @sorted = sort {$inner->{$a} <=> $inner->{$b}} keys %$inner;

# print in sorted order

foreach my $ikey (@sorted) {

printf "\$a{'$okey'}{'$ikey'}=$inner->{$ikey};\n";

}

}

Andrey

with position. Although you can _output_ hash elements in sorted order by converting hash to list of hash keys,

and sorting this list:

my %a;

$a{'x1'}{'d'}=5;

$a{'x1'}{'a'}=2;

$a{'x1'}{'g'}=1;

$a{'x2'}{'x'}=5;

$a{'x2'}{'g'}=2;

$a{'x3'}{'r'}=13;

$a{'x3'}{'c'}=3;

$a{'x3'}{'b'}=4;

$a{'x3'}{'y'}=18;

# $okey iterates thru all 'outer' hash keys ordered

# by their names

foreach my $okey (sort keys %a) {

# $inner is a reference to anonymous hash

# (2nd level of your data structure)

my $inner = $a{$okey};

# @sorted contains 'inner' keys ordered by values

my @sorted = sort {$inner->{$a} <=> $inner->{$b}} keys %$inner;

# print in sorted order

foreach my $ikey (@sorted) {

printf "\$a{'$okey'}{'$ikey'}=$in

}

}

Andrey