in reply to Comparing spaceships (cmp and <=> as options)

sub returns a code reference on its own, no need to add a backslash. You have to add parameters to the subs, though. The canonical ones for sort are $a and $b. Localize them to the value to be sorted on so that the code still returns the full record.
#!/usr/bin/perl use warnings; use strict; my $sort = 'hours'; # really comes from a switch my %map = ( # sorting map hours => [0, sub {$a <=> $b}], code => [1, sub {$a cmp $b}], name => [2, sub {$a cmp $b}], ); # example data my @records = ( [10, 'xyz232', 'secret project'], [ 5, 'foo123', 'world domination'], [ 7, 'bar666', 'have a beer'], ); for ( sort { local ($a, $b) = map $_->[$map{$sort}->[0]], $a, $b; $map{$sort}[1]->(); } @records ) { print "@$_\n"; }
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ