in reply to Sort Array of Hashes based on Hash value

One good place to start is perlfaq4, specifically How do I sort a hash (optionally by value instead of key)?.

Another reference is the documentation for sort itself.

I'm not sure what you mean by sorting the array based on those keys in that order. Perhaps you could provide an example data structure before and after the sort?

  • Comment on Re: Sort Array of Hashes based on Hash value

Replies are listed 'Best First'.
RE: Re: Sort Array of Hashes based on Hash value
by raflach (Pilgrim) on Jun 07, 2000 at 03:50 UTC
    %{array[1]} = { key1 => 3, key2 => 2, key3 => "test" }; %{array[2]} = { key1 => 2, key2 => 1, key3 => "test2" }; %{array[3]} = { key1 => 3, key2 => 1, key3 => "test3" }; %{array[4]} = { key1 => 1, key2 => 1, key3 => "test4" }; &sortarrayofhashes(\@array,"key1","key2");
    now the arrays look like this:
    # NOTE, the following is not perlcode, just statement of fact $array[1]{key1} == 1 $array[1]{key2} == 1 $array[1]{key3} == test4 $array[2]{key1} == 2 $array[2]{key2} == 1 $array[2]{key3} == test2 $array[3]{key1} == 3 $array[3]{key2} == 1 $array[3]{key3} == test3 $array[4]{key1} == 3 $array[4]{key2} == 2 $array[4]{key3} == test
    So that's the result I'm looking to be able to obtain. THnaks