use strict; use diagnostics; sub by_seq { my $seq_aref = shift; my %seq_helper; { my $index = 0; for my $item (@{ $seq_aref }) { $seq_helper{$item} = $index; $index++; }; }; return $seq_helper{$a} <=> $seq_helper{$b}; }; # -- my %h = (one=>2,three=>4,five=>6,seven=>8,nine=>0); for (sort by_seq([qw(five nine one seven three)]) keys %h) { print "$_: $h{$_}\n"; }; __DATA__ expected output five: 6 nine: 0 one: 2 seven: 8 three: 4