Solution for a sorting problem.