oakbox has asked for the wisdom of the Perl Monks concerning the following question:
$sort_line_contents->{element_one}->{element_two}
I want to reverse sort on element_one (numerically) and then sort normally on element_two. Here is an example of my data set:
$sort_line_contents->{1}->{2} = Item1; $sort_line_contents->{0}->{3} = Item2; $sort_line_contents->{1}->{4} = Item3; $sort_line_contents->{1}->{5} = Item4; $sort_line_contents->{2}->{6} = Item5;
I want to take that information and force it into an order like this:
Item5
Item1
Item3
Item4
Item2
My first crack at this was using a sort statement like so:
my @hash_keys2 = sort { %{$sort_line_contents}->{$b} <=> %{$sort_li +ne_contents}->{$a} } keys %{$sort_line_contents};
I'm even more confused because the second sort using the second value works AS EXPECTED:foreach (reverse sort keys %{$sort_line_contents}) { push(@hash_keys2, +$_);}
My question is, why doesn't the sort on the hash ref give me the expected behaviour? Is there a problem with the way I am expanding my $sort_line_contents reference? I see that it is reading out the correct VALUES, it is just putting them in an unexpected order. I get the same unexpected order with the <=> and the cmp operators???foreach my $hk (@hash_keys2){ my @filli = sort { $sort_line_contents->{$hk}->{$a} <=> $sort_li +ne_contents->{$hk}->{$b} } keys %{$sort_line_contents->{$hk}}; foreach (@filli){ #output to other part of program } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sort not acting as expected on Hash Ref
by gjb (Vicar) on Jan 01, 2003 at 11:37 UTC | |
|
Re: Sort not acting as expected on Hash Ref
by jdporter (Paladin) on Jan 02, 2003 at 04:53 UTC | |
|
Re: Sort not acting as expected on Hash Ref
by PodMaster (Abbot) on Jan 01, 2003 at 11:29 UTC | |
by pg (Canon) on Jan 01, 2003 at 17:44 UTC |