in reply to Array of hash sorting problem

This is a FAQ. Typing perldoc -q sort or looking at perlfaq4 tells you the answer.

Replies are listed 'Best First'.
Re^2: Array of hash sorting problem
by thillai_selvan (Initiate) on Mar 25, 2010 at 09:56 UTC
    I know this. I know about sort. I am facing the problem in dereferencing the array. Can you please explain the clear way.

      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.

      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