in reply to Re: Array of hash sorting problem
in thread Array of hash sorting problem

I know this. I know about sort. I am facing the problem in dereferencing the array. Can you please explain the clear way.

Replies are listed 'Best First'.
Re^3: Array of hash sorting problem
by Corion (Patriarch) on Mar 25, 2010 at 10:10 UTC

    You don't need to dereference the array, because the array elements to compare will be stored in $a and $b. All you need to do is to extract the fields of your array elements and compare them. See, again, the examples in perlfaq4, and also maybe References Quick Reference. Maybe you want to show the code you have written and explain where you actually have the problems instead of trying to make us write your code for you.

Re^3: Array of hash sorting problem
by Marshall (Canon) on Mar 25, 2010 at 10:15 UTC
    Each element in the array is a reference to an anon hash. Loop over these references, then deference to get the keys to put into sort. output of sort is sorted keys. see below.
    use strict; use warnings; my @aoh =( { 3 => 15, 4 => 8, 5 => 9, }, { 3 => 11, 4 => 25, 5 => 6, }, { 3 => 5, 4 => 18, 5 => 5, }, { 0 => 16, 1 => 11, 2 => 7, }, { 0 => 21, 1 => 13, 2 => 31, }, { 0 => 11, 1 => 14, 2 => 31, }, ); foreach my $href (@aoh) { my @sorted_keys = sort {$href->{$a} <=> $href->{$b} } keys %$href; foreach my $key (@sorted_keys) { print "$key => $href->{$key}\n" } print "\n"; } __END__ 4 => 8 5 => 9 3 => 15 5 => 6 3 => 11 4 => 25 3 => 5 5 => 5 4 => 18 2 => 7 1 => 11 0 => 16 1 => 13 0 => 21 2 => 31 0 => 11 1 => 14 2 => 31