my %order; $order{$order[$_]} = $_+1 for (0..$#order); my @sorted = sort { ($order{$a}||@order) <=> ($order{$b}||@order) } @sortme;