in reply to Re: sorting hash by a certain value (that it is in an array)
in thread sorting hash by a certain value (that it is in an array)

this is not about sorting an array, it's about sorting an hash that has arrays in it. the example by brx created a new sorted array @sorted_books. I need the %books hash to be sorted as so:
%books=( '54' => ['enlgish', 'bob falicas', 17], '34' => ['enlgish', 'dani M. rod', 54], '24' => ['spanish', 'ramk rovale', 41], );
I don't see how the example by brx gives me that. when I print out the %books hash using this:
while (my($key, $value) = each(%books)){ print "$key: $value\n"; }
I get the same unsorted result.

Replies are listed 'Best First'.
Re^3: sorting hash by a certain value (that it is in an array)
by Corion (Patriarch) on Jul 17, 2012 at 13:34 UTC
Re^3: sorting hash by a certain value (that it is in an array)
by jethro (Monsignor) on Jul 17, 2012 at 13:35 UTC

    A hash is a different data structure than an array. Its advantage is that it allows arbitrary strings as keys instead of just a number. The disadvantage is that it can't be sorted. Read about "hash tables" on wikipedia if you want to know why.

Re^3: sorting hash by a certain value (that it is in an array)
by clueless newbie (Curate) on Jul 17, 2012 at 16:06 UTC
    Perhaps you could use Tie::IxHash?
    my %unordered=(...); tie my %ordered, "Tie::IxHash"; for my $key (sort {$unordered{$a}[1] cmp $unordered{$b}[1]} keys %$ +unordered) { $ordered{$key}=$unordered->{$key}; };
Re^3: sorting hash by a certain value (that it is in an array)
by brx (Pilgrim) on Jul 17, 2012 at 15:50 UTC