my @list = sort { @$a <=> @$b or join(' ', reverse @$a) cmp join(' ', reverse @$b) } @input;