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

hey there, i'm new to the monastary, so i don't know if my problem was already dealt with once...at least i didn't find anything. anyway, i wanna sort a hash of this kind: $hash{$id} = [$lName,$fName,$dName,$priMail,$secMail,$hp]; ascending by the value $dName, which should be $hash{$id}->[2]. now, ist ist prossible to supply a sub to the sort routine so it will sort the hash correctly, or do i have to write an own algorithm? thanks for your help... sanchez

20030809 Edit by jeffa: Changed title from 'sort hasehs.... '

Replies are listed 'Best First'.
Re: sort hashes....
by diotalevi (Canon) on Aug 09, 2003 at 13:25 UTC

    Run this command

    perldoc -q 'sort a hash'
Re: sort hashes....
by CombatSquirrel (Hermit) on Aug 09, 2003 at 15:43 UTC
    You ought to get the camel book. Basic programming concepts such as sorting are explained in depth there.
    Anyway, the code is @sorted = sort { $hash{$a}->[2] <=> $hash{$b}->[2] } keys hash;.
    Hope that helped.
Re: sort hashes....
by Zaxo (Archbishop) on Aug 10, 2003 at 05:42 UTC

    You can produce an array of keys sorted as you wish, my @sorted_keys = sort {$hash{$a}->[2] cmp $hash{$b}->[2]} keys %hash; That can subsequently be used in a hash slice.

    After Compline,
    Zaxo

      Thanks for your help so far :) I already tried exactly the same code you suggest before, but the problem is I need to sort the hash accordingly (is this the right word, sorry i'm german :) ); i don't need a sorted list containing the strings. any ideas? sanchez
        hmmmm.....dammit can you even sort hashes...? lol thanks anyway :-)