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

Hi Monks!
I wanted to ask you all if it is possible to sort a hash in a specific order... for example if i have a hash with this keys
A B C D E F G H I K L M N P Q R S T V W X Y Z
and i want to order them in this specific order like this
A R N D C Q E G H I L K M F P S T W Y V B Z X
thanks fot the help!

Replies are listed 'Best First'.
Re: sorting hash in a non alpabellical order
by Corion (Patriarch) on Jan 13, 2014 at 11:35 UTC

    A hash has no order.

    But maybe you want to output the elements of a hash in a particular order. Then you can use the following approach with a "hash slice" for example:

    my @columns= qw(A R N D C Q E G H I L K M F P S T W Y V B Z X); my %hash= ...; print "@columns\n"; print "@hash{ @columns }\n";

    The alternative approach is to simply use a loop over @columns.

      thank you! this helps me a lot :)