in reply to Re: Sorting an hash array
in thread Sorting an hash array

Hi, I've tryed the code you gave me: same disorded result.... Regards, Kepler

Replies are listed 'Best First'.
Re^3: Sorting an hash array
by AnomalousMonk (Archbishop) on Jul 26, 2014 at 00:26 UTC

    I don't see offhand what would be wrong with AppleFritter's code, but here's some code closer to your OP, at least as regards sorting. Critically, IMHO, this code includes example data. Can you say where the 'disorder' creeps in? (In particular, since the sort comparison in AppleFritter's code is lexic versus numeric in the original, how can it be the same disorder?)

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my %funcs = qw(1 a 2 aa 3 aaa 4 z 5 zz 6 b 7 bb); dd \%funcs; ;; my @sorted_keys = sort { $funcs{$a} cmp $funcs{$b} } keys %funcs; dd \@sorted_keys; ;; my @sorted_vals = @funcs{@sorted_keys}; dd \@sorted_vals; ;; print qq{'$_' -> '$funcs{$_}'} for @sorted_keys; " { 1 => "a", 2 => "aa", 3 => "aaa", 4 => "z", 5 => "zz", 6 => "b", 7 => + "bb" } [1, 2, 3, 6, 7, 4, 5] ["a", "aa", "aaa", "b", "bb", "z", "zz"] '1' -> 'a' '2' -> 'aa' '3' -> 'aaa' '6' -> 'b' '7' -> 'bb' '4' -> 'z' '5' -> 'zz'

Re^3: Sorting an hash array
by BillKSmith (Monsignor) on Jul 26, 2014 at 04:31 UTC
    Did you understand that AppleFritters code does not (and cannot) fix the hash - only the output?
    Bill