megaurav2002 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, Can anyone please tell me how to use MAP to sort the keys in the HASH? Thanks!

Replies are listed 'Best First'.
Re: sorting keys in Hash
by GrandFather (Saint) on May 11, 2007 at 02:27 UTC

    Tell us what you are actually trying to do. Map is not generally appropriate in itself for sorting (sort does that job), but you may be trying to do something interesting like a Schwartzian transform (see replies to What is "Schwarzian Transform" (aka Schwartzian)). Give us a good question (some context and / or code would help a lot) and we'll give you a good answer.


    DWIM is Perl's answer to Gödel
Re: sorting keys in Hash
by friedo (Prior) on May 11, 2007 at 02:06 UTC
    You'd probably have more luck using sort.
Re: sorting keys in Hash
by Trizor (Pilgrim) on May 11, 2007 at 04:24 UTC
    my @sorted_keys = sort keys %hash; my @sorted_vals = @hash{@sorted_keys};

    does the trick with out a map. Hash keys are internally sorted after they've gone through the hashing algorithm, which usually results in an order that is Not Useful to You.

Re: sorting keys in Hash
by jesuashok (Curate) on May 11, 2007 at 07:10 UTC