in reply to need to sort array of hash

Have you read the FAQ How do I sort an array by (anything)?

Replies are listed 'Best First'.
Re^2: need to sort array of hash
by Anonymous Monk on Dec 22, 2018 at 11:10 UTC

    yes, i red, but didn't any clue.

    need to do something like this:

    $Arrayofhash[$key]->{$a}->{hyundai} <=>  $Arrayofhash[$key]->{$b}->{hyundai}

      Brother dsheroh has shown the correct comparison for the dataset you have provided. However, I agree with my fellow monk that the data structure is horrible. If I had to deal with this I would probably refactor it to be something more like

      my $ArrayofHash = [ { name => 'TaskA', attrs => { 'maruti' => '20', 'honda' => '25', 'zen' => '25', 'hyundai' => '35', 'ford' => '22', 'toyota' => '11' } }, { name => 'TaskB', attrs => { 'maruti' => '11', 'honda' => '22', 'zen' => '33', 'hyundai' => '33', 'ford' => '24', 'toyota' => '16' } }, { name => 'TaskC', attrs => { 'maruti' => '12', 'honda' => '22', 'zen' => '33', 'hyundai' => '44', 'ford' => '55', 'toyota' => '66' } } ];

      This way all of your hashrefs have known keys so it's an easier comparison to codify. eg. $a->{attrs}->{hyundai} <=> $b->{attrs}->{hyundai}

      It depends on what your real dataset looks like whether or not this is an advisable approach.