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

Hi All,

I have an hash %hoa like this,
$VAR1 = { 'userid-000003' => [ 'userid-000003', ' Thur sep 15 04:36:48 PDT 2005' +, ' Running', ' Thur sep 15 04:47:52 PDT 2005' ], 'userid-000002' => +[ 'userid-000002', ' Thur sep 15 04:36:48 PDT 2005', ' Running', ' Th +ur sep 15 04:47:52 PDT 2005' ], 'userid-000001' => [ 'userid-000001', + ' Thur sep 15 04:36:48 PDT 2005', ' Running', ' Thur sep 15 04:47:52 + PDT 2005' ], 'userid-000004' => [ 'userid-000004', ' Thur sep 15 02: +46:48 PDT 2005', ' Completed', ' Thur sep 15 02:57:52 PDT 2005' ] };

I need to sort this hash based on the values in either of the column.
That is my hash key refers to,
job_id => [job_id, submit_time, status, complete_time].
How could I sort it based on job_id (or) submit_time (or) status (or) complete_time.

Please help me in this.
Senthil

Edited by planetscape - formatting

Replies are listed 'Best First'.
Re: sort hash based on values
by GrandFather (Saint) on Oct 28, 2005 at 09:27 UTC
Re: sort hash based on values
by rsennat (Beadle) on Oct 28, 2005 at 09:31 UTC

    yeah rite. get the keys based on the sorted values.

    i got it now. here it is,
    @sort_keys = sort { $hoa{$a}[0] cmp $hoa{$b}[0] } keys %hoa;

    Thanks
    Senthil
Re: sort hash based on values
by jesuashok (Curate) on Oct 28, 2005 at 09:33 UTC
    Hi,

    I can tell you the way how you have to do this.

    1) Traverse in Hash.

    2) derefrence the value since that is array reference.

    3) then sort the array using sort function sort @$value derefrenceing

    4) then you do the rest as per your requirement

    "Keep pouring your ideas"