in reply to Printing a Tree of Hash of hashes sorted by value

#!/usr/bin/perl use warnings; use strict; my %myData= ( 'Method1' => { 'Name' => 'ABC', 'File' => 'Beta.dat', 'Category' => 'Methods', 'Params' => 'ARGV' }, 'Method2' => { 'Name' => 'MNO', 'File' => 'Alpha.dat', 'Category' => 'Functions', 'Params' => 'ARGV' }, 'Method3' => { 'Name' => 'ABA', 'File' => 'Beta.dat', 'Category' => 'Methods', 'Params' => 'ARGV' }, 'Method4' => { 'Name' => 'MNZ', 'File' => 'Alpha.dat', 'Category' => 'Functions', 'Params' => 'ARGV' } ); my @unsorted = keys %myData; my @sorted = sort { $myData{$a}{'Category'} cmp $myData{$b}{'Category'} or $myData{$a}{'File'} cmp $myData{$b}{'File'} or $myData{$a}{'Name'} cmp $myData{$b}{'Name'} } @unsorted; print join(' ', @sorted),"\n"; #prints Method2 Method4 Method3 Method1

This works because cmp gives back 0 on equality. Which means that the 'or' doesn't short circuit and tests the next comparision.

I dropped the 'reverse' from the sort and exchanged $a and $b of the cmp instead.