in reply to sort HoA by tow keys
Since the intent seems to be to do a multi-key sort (first by date, then by time and then by hash key), this intent is more clearly and maintainably implemented by sorting in just this way.
Although the main benefit is clarity, there may also be a side-benefit of better performance, since the relatively expensive (as well as messy) arithmetic operations are not needed for each comparison of the sort.
If performance is really an issue due to hash size, a ST or GRT approach may be merited. See A Fresh Look at Efficient Perl Sorting and also Old sorting paper holds the key to unlocking the secrets of the Schwartzian Transform for a discussion of and general comments on both approaches.
Sort by combined date-time, then key:
Sort explicitly by date, time, key:>perl -wMstrict -le "my %HoA = ( 2 => ['cd', 20090101, 91010], 3 => ['ef', 20090201, 101000], 1 => ['ab', 20090101, 91010], ); my @ordered_keys = sort { ($HoA{$a}[1]*1000000+$HoA{$a}[2]) <=> ($HoA{$b}[1]*1000000+$HoA{$ +b}[2]) || $a <=> $b } keys (%HoA); for my $key (@ordered_keys) { print qq{$key -> [@{$HoA{$key}}]} } " 1 -> [ab 20090101 91010] 2 -> [cd 20090101 91010] 3 -> [ef 20090201 101000]
>perl -wMstrict -le "my %HoA = ( 2 => ['cd', 20090101, 91010], 3 => ['ef', 20090201, 101000], 1 => ['ab', 20090101, 91010], ); my @ordered_keys = sort { $HoA{$a}[1] <=> $HoA{$b}[1] || $HoA{$a}[2] <=> $HoA{$b}[2] || $a <=> $b } keys %HoA; for my $key (@ordered_keys) { print qq{$key -> [@{$HoA{$key}}]} } " 1 -> [ab 20090101 91010] 2 -> [cd 20090101 91010] 3 -> [ef 20090201 101000]
|
|---|